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 

std::map questions

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
andy.canfield@gmail.com
Guest





PostPosted: Thu Oct 27, 2005 9:29 am    Post subject: std::map questions Reply with quote



I have two questions about std::map.

First question: If I have a std::map with an iterator pointing to
begin() and I insert an element into the map using insert() is my
iterator garaunteed to still be pointing at the same place it was when
I set it to begin()? I have the book "The C++ Standard Library: A
tutorial and reference" but I can't find anywhere where it says yes or
no on this.

Second question: If I traverse a std::map using an iterator and the map
sort criteria was "less than" am I garaunteed that it will traverse
from least to greatest if I traverse from begin() to end() or is it
only garaunteed that I will touch all elements in the map?

Thank you,
Andy


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ko van der Sloot
Guest





PostPosted: Thu Oct 27, 2005 11:15 am    Post subject: Re: std::map questions Reply with quote



[email]andy.canfield (AT) gmail (DOT) com[/email] wrote:
Quote:
I have two questions about std::map.

First question: If I have a std::map with an iterator pointing to
begin() and I insert an element into the map using insert() is my
iterator garaunteed to still be pointing at the same place it was when
I set it to begin()? I have the book "The C++ Standard Library: A
tutorial and reference" but I can't find anywhere where it says yes or
no on this.

did you see table 6.33 on page 227?

Quote:
Second question: If I traverse a std::map using an iterator and the map
sort criteria was "less than" am I garaunteed that it will traverse
from least to greatest if I traverse from begin() to end() or is it
only garaunteed that I will touch all elements in the map?

I would be very suprised if the traverse would be unsorted.
Can't find the right wording in Josuttis right now.
Quote:
Thank you,
Andy

Ko

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Tony Delroy
Guest





PostPosted: Thu Oct 27, 2005 11:16 am    Post subject: Re: std::map questions Reply with quote



Hi Andy,

SGI provide an excellent reference to their implementation of the STL,
see http://www.sgi.com/tech/stl/Map.html for the map page. Note their
promise:

Quote:
Map has the important property that inserting a new element
into a map does not invalidate iterators that point to existing
elements. Erasing an element from a map also does not
invalidate any iterators, except, of course, for iterators that
actually point to the element that is being erased.

So, regarding your first question: the iterator you assigned from
begin() will still point to the same element (though that may not be
the beginning element any more) after an insert() IF IT EVER POINTED TO
AN ELEMENT IN THE FIRST PLACE. If the map had been empty, begin()
would have returned the save value end() would have, and this could be
virtually anything. In summary, don't use the original value unless
you know it wasn't equal to end() at the time it was retrieved.

Re Q2. The sort order is guaranteed.

Regards,

Tony


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ulrich Eckhardt
Guest





PostPosted: Thu Oct 27, 2005 11:16 am    Post subject: Re: std::map questions Reply with quote

[email]andy.canfield (AT) gmail (DOT) com[/email] wrote:
Quote:
First question: If I have a std::map with an iterator pointing to
begin() and I insert an element into the map using insert() is my
iterator garaunteed to still be pointing at the same place it was when
I set it to begin()?

1. The iterator still points to the same element.
2. If the new element sorts before the one it points to, it will not point
to the beginning of the sequence, but I don't think that's what you meant.

Quote:
I have the book "The C++ Standard Library: A tutorial and reference" but
I can't find anywhere where it says yes or no on this.

I don't know that book, but maybe you just don't see it, the thing is called
'iterator invalidation'.

Quote:
Second question: If I traverse a std::map using an iterator and the map
sort criteria was "less than" am I garaunteed that it will traverse
from least to greatest if I traverse from begin() to end() or is it
only garaunteed that I will touch all elements in the map?

Yes, this is guaranteed, not only for map but also multimap, set and
multiset.

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Francis Glassborow
Guest





PostPosted: Thu Oct 27, 2005 2:05 pm    Post subject: Re: std::map questions Reply with quote

In article <1130362302.342506.119480 (AT) g14g2000cwa (DOT) googlegroups.com>,
[email]andy.canfield (AT) gmail (DOT) com[/email] writes
Quote:
First question: If I have a std::map with an iterator pointing to
begin() and I insert an element into the map using insert() is my
iterator garaunteed to still be pointing at the same place it was when
I set it to begin()?

Same place in what sense? You have no control on where a new element
will be added and the result may need radical restructuring of the
internal representation. I would expect the iterator to still locate
some element of the map but not necessarily the one you might expect,
and not always the one that is now identified by dereferencing the value
returned by a new call to begin().


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) 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.