470,833 Members | 1,220 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

std::map iterators

In an std::map, iterators do not become invalid if other elements are
added erased. Just to make sure, does that mean that in the following
code:

map<string,Bm;
B *b = &(m["something"]);

The address in 'b' will remain valid, and always point to that same
object, as long as that data isn't erased from the map?

Similarily:

map<string,Bm;
map<string,B>::iterator i(m.find("something"));
B *b = (i == m.end()) ? NULL : &((*i).second);

Will b there also always remain valid as long as "something" remains
in the map?

Thanks,
Jason
Jun 27 '08 #1
4 1542
Hi!

ja************@gmail.com schrieb:
The address in 'b' will remain valid, and always point to that same
object, as long as that data isn't erased from the map?
Yes.
Will b there also always remain valid as long as "something" remains
in the map?
Yes.

Regards,
Frank
Jun 27 '08 #2
On May 21, 6:20 pm, Frank Birbacher <bloodymir.c...@gmx.netwrote:
[snip]
Thanks!
Jun 27 '08 #3
ja************@gmail.com wrote:
In an std::map, iterators do not become invalid if other elements are
added erased. Just to make sure, does that mean that in the following
code:

map<string,Bm;
B *b = &(m["something"]);

The address in 'b' will remain valid, and always point to that same
object, as long as that data isn't erased from the map?
Yes, it does.
Similarily:

map<string,Bm;
map<string,B>::iterator i(m.find("something"));
B *b = (i == m.end()) ? NULL : &((*i).second);

Will b there also always remain valid as long as "something" remains
in the map?
Yes.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #4
ja************@gmail.com wrote:
In an std::map, iterators do not become invalid if other elements are
added erased. Just to make sure, does that mean that in the following
code:

map<string,Bm;
B *b = &(m["something"]);

The address in 'b' will remain valid, and always point to that same
object, as long as that data isn't erased from the map?
Formally, validity of iterators and references into a map are separate
issues.

However, you are lucky: the standard also guarantees that references into a
map remain valid. Now, one could argue that reference validity does not
imply pointer validity. However, I think that is a little stretch. So 'b'
will remain valid. If you want to be on the formally safe side, you could
do something like:

B& b = m["something"];

Similarily:

map<string,Bm;
map<string,B>::iterator i(m.find("something"));
B *b = (i == m.end()) ? NULL : &((*i).second);

Will b there also always remain valid as long as "something" remains
in the map?
With the caveats from above: yes.
Best

Kai-Uwe Bux

Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Moetman | last post: by
26 posts views Thread by Pieter Thysebaert | last post: by
1 post views Thread by rg | last post: by
17 posts views Thread by Gernot Frisch | last post: by
3 posts views Thread by Christian Christmann | last post: by
4 posts views Thread by kalita | last post: by
2 posts views Thread by brutus | last post: by
6 posts views Thread by brzrkr0 | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.