473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

stl c++ & map

Hi, I am implementing a version of the C++ STL map class for a school
assignment and have a question... what is the difference between
const_iterator and just iterator??... If I already have an iterator
implemented, can I just copy that code and change return types to const
and would that be sufficient?? I am lost 'cuz I don't know this
definition!

Thanks in advance! :)
Jul 23 '05 #1
7 2712

"lmanchur." <lm******@gmail.com> wrote in message
news:23**************************@posting.google.c om...
Hi, I am implementing a version of the C++ STL map class for a school
assignment and have a question... what is the difference between
const_iterator and just iterator??...
'iterator' should allow modification of the object to which
it refers. 'const_iterator' should disallow modification
(you can do via 'help' from the language's 'const' qualifier,
or simply implement the prevention in your logic).
If I already have an iterator
implemented, can I just copy that code
The code will be very similar, yes.
and change return types to const
and would that be sufficient??
It might be, it might not. That depends upon the details
of your implementation.
I am lost 'cuz I don't know this
definition!


The best explanation I've seen is in this book:
www.josuttis.com/libbook

You can also get some ideas from looking at your standard
library header files. E.g. look at header <map>

-Mike
Jul 23 '05 #2
lmanchur. wrote:
Hi, I am implementing a version of the C++ STL map class for a school
assignment and have a question... what is the difference between
const_iterator and just iterator??... If I already have an iterator
implemented, can I just copy that code and change return types to const
and would that be sufficient?? I am lost 'cuz I don't know this
definition!


This is when a little test code might help...

#include <iostream>
#include <ostream>
#include <map>
int main()
{
typedef std::map<int,int> T;
T l_map;
T::const_iterator l_iter;
T::iterator l_iter2;

l_map[1] = 1;
l_map[2] = 2;

l_iter2 = l_map.begin();
l_iter = l_iter2;

l_iter2->second = 3;
l_iter->second = 4;
}
Jul 23 '05 #3
See Scott Meyers' article, "Three Guidelines for Effective Iterator
Use". You can find it on his website:
http://www.aristeia.com/publications_frames.html.

Jul 23 '05 #4
Thanks so much to all who responded but especially to Gianni!!! That
really helped illustrate the point and gave me a basis for which to
test my own.

Thanks again!!!!

Jul 23 '05 #5
Actually if I can ask another question.... i thought i had it working
but it turns out I only partially have it working.... for some reason
it's not figuring out which begin() method to use... I have the
following methods:

iterator begin();
const_iterator begin() const;
And if I rename the second one to something like cbegin() it works
exactly as the std::map::const_iterator begin(); does. But if I leave
it as defined above I get the following compile error:

StrIntMap.cpp: In function `static void StrIntMap::test()':
StrIntMap.cpp:57: no match for `StrIntMap::const_iterator & =
StrIntMap::iterator'
StrIntMap.hh:150: candidates are: class StrIntMap::const_iterator &
StrIntMap::const_iterator::operator =(const StrIntMap::const_iterator
&)
gmake: *** [StrIntMap.o] Error 1
Any thoughts??

Thanks alot,
Lee

Jul 23 '05 #6
lm******@gmail.com wrote:
Actually if I can ask another question.... i thought i had it working
but it turns out I only partially have it working.... for some reason
it's not figuring out which begin() method to use... I have the
following methods:

iterator begin();
const_iterator begin() const;
And if I rename the second one to something like cbegin() it works
exactly as the std::map::const_iterator begin(); does. But if I leave
it as defined above I get the following compile error:

StrIntMap.cpp: In function `static void StrIntMap::test()':
StrIntMap.cpp:57: no match for `StrIntMap::const_iterator & =
StrIntMap::iterator'
StrIntMap.hh:150: candidates are: class StrIntMap::const_iterator &
StrIntMap::const_iterator::operator =(const StrIntMap::const_iterator
&)
gmake: *** [StrIntMap.o] Error 1
Any thoughts??


The error says it can't convert an "iterator" to a "const_iterator &"
(note the reference .. &) !

This is a different problem.

In this case it looks like you have somthing like (speculating):

M::const_iterator & foo()
{
M::iterator x;

return x;
}

A reference is kind of a special thing in C++, it like a pointer but
looks like an object. The speculation code above tries to return a
reference (pointer) to an object that it needs to create (temporary) but
since it's returning from a function, there is no way to allocate one
without it being destroyed.

Try returning by value.

M::const_iterator foo()
{
M::iterator x;

return x;
}

Now, you can make a copy of the iterator.

You could also generate a cons_iterator (by calling begin() const) but
you still have a problem with returning temporaries. (look in the FAQ).
#include <iostream>
#include <ostream>
#include <map>
int main()
{

typedef std::map<int,int> T;
T l_map;

const T & l_const_map;

l_map[1] = 1;
l_map[2] = 2;

T::const_iterator l_iter3 = l_const_map.begin();

T::iterator l_iter3 = l_const_map.begin(); // illegal.

}
Jul 23 '05 #7
No, that's what I have. Again the error is:

StrIntMap.cpp: In function `static void StrIntMap::test()':
StrIntMap.cpp:136: no match for `StrIntMap::const_reverse_iterator & =
StrIntMap::iterator'
StrIntMap.hh:318: candidates are: class
StrIntMap::const_reverse_iterator &
StrIntMap::const_reverse_iterator::operator =(const
StrIntMap::const_reverse_iterator &)
gmake: *** [StrIntMap.o] Error 1
obelix[93]%

But my code is this:
StrIntMap::const_iterator StrIntMap::lower_bound(const
StrIntMap::key_type& k) const{

StrIntMap::const_iterator it;

for(it=this->cbegin(); it!=this->cend(); it++)
if((it->first)>=k)
return it;
return it;
}

I already have a method called lower_bound that returns an iterator.
If I rename the above method to something else like clower_bound it
works perfectly as required.

Any additional thoughts?

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Collin VanDyck | last post by:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem.. I am trying to get a Java/Xalan transform to pass through a numeric character...
1
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
0
by: Thomas Scheffler | last post by:
Hi, I runned in trouble using XALAN for XSL-Transformation. The following snipplet show what I mean: <a href="http://blah.com/?test=test&amp;test2=test2">Test1&amp;</a> <a...
4
by: Luklrc | last post by:
Hi, I'm having to create a querysting with javascript. My problem is that javscript turns the "&" characher into "&amp;" when it gets used as a querystring in the url EG: ...
4
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
11
by: Jeremy | last post by:
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.