By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

stl c++ & map

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.