C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

stl c++ & map

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
lmanchur.
Guest





PostPosted: Sat Jan 29, 2005 9:17 pm    Post subject: stl c++ & map Reply with quote



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! Smile
Back to top
Mike Wahler
Guest





PostPosted: Sat Jan 29, 2005 9:32 pm    Post subject: Re: stl c++ & map Reply with quote




"lmanchur." <lmanchur (AT) gmail (DOT) com> wrote

Quote:
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).

Quote:
If I already have an iterator
implemented, can I just copy that code

The code will be very similar, yes.

Quote:
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.

Quote:
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



Back to top
Gianni Mariani
Guest





PostPosted: Sat Jan 29, 2005 9:33 pm    Post subject: Re: stl c++ & map Reply with quote



lmanchur. wrote:
Quote:
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;
}

Back to top
Matthew Schaefer
Guest





PostPosted: Sat Jan 29, 2005 9:38 pm    Post subject: Re: stl c++ & map Reply with quote

See Scott Meyers' article, "Three Guidelines for Effective Iterator
Use". You can find it on his website:
http://www.aristeia.com/publications_frames.html.

Back to top
lmanchur@gmail.com
Guest





PostPosted: Sun Jan 30, 2005 2:49 pm    Post subject: Re: stl c++ & map Reply with quote

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

Back to top
lmanchur@gmail.com
Guest





PostPosted: Sun Jan 30, 2005 3:01 pm    Post subject: Re: stl c++ & map Reply with quote

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

Back to top
Gianni Mariani
Guest





PostPosted: Sun Jan 30, 2005 4:12 pm    Post subject: Re: stl c++ & map Reply with quote

[email]lmanchur (AT) gmail (DOT) com[/email] wrote:
Quote:
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.

}



Back to top
lmanchur@gmail.com
Guest





PostPosted: Sun Jan 30, 2005 5:12 pm    Post subject: Re: stl c++ & map Reply with quote

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?

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.