stl c++ & map

L

lmanchur.

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! :)
 
M

Mike Wahler

lmanchur. said:
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
 
G

Gianni Mariani

lmanchur. said:
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;
}
 
L

lmanchur

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!!!!
 
L

lmanchur

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::eek:perator =(const StrIntMap::const_iterator
&)
gmake: *** [StrIntMap.o] Error 1


Any thoughts??

Thanks alot,
Lee
 
G

Gianni Mariani

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::eek:perator =(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.

}
 
L

lmanchur

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::eek:perator =(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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top