Do insert / erase invalidate pointers to elements (values) of std::unordered_map?

K

K. Frank

Hello Group!

I believe that if I insert a key / value pair into a
std::unordered_map, and then take a pointer to the
inserted value, that pointer will remain valid after
subsequent calls to insert and erase.

(I understand that erasing a value will invalidate the
pointer to that specific value. But I believe that
other pointers will not be invalidated.)

I would like to check whether or not this belief is correct.
I believe that the standard says this, but the language
is not really clear to me.

(unordered_map is the case of interest to me, but I
believe that this is true for all of the so-called
associative containers.)

From the draft standard (n3485, pg. 723):

Point 9 of 23.2.4 [associative.reqmts] states:

9 The insert and emplace members shall not affect the
validity of iterators and references to the container,
and the erase members shall invalidate only iterators
and references to the erased elements.

The language is a little terse. It speaks of references,
but not pointers, and of "references to the container,"
rather than references to elements of (or values in) the
container.

Do I have it right that pointers to values in an
unordered_map will remain valid?


Thanks.


K. Frank
 
K

K. Frank

Hi Language Experts!

Hello Group!

I believe that if I insert a key / value pair into a
std::unordered_map, and then take a pointer to the
inserted value, that pointer will remain valid after
subsequent calls to insert and erase.
...

Could anyone with standard-reading skills help verify
what the standard says? I believe that pointers to
unordered_map values remain valid, but I'm really not
certain based on the terse language I've found in the
standard.


Thanks.


K. Frank
 
Ö

Öö Tiib

Could anyone with standard-reading skills help verify
what the standard says? I believe that pointers to
unordered_map values remain valid, but I'm really not
certain based on the terse language I've found in the
standard.

If with "inserted value" you mean
that pair<key_type const, mapped_type> in map then
pointer to it remains valid on case of subsequent erases
and inserts of other elements.
 
K

K. Frank

Hello Öö!

If with "inserted value" you mean
that pair<key_type const, mapped_type> in map then
pointer to it remains valid on case of subsequent erases
and inserts of other elements.

Thank you for the confirmation.

By "inserted value" I actually had in mind the mapped_type,
i.e.,
pointer_to_mapped_type =
&(pair<key_type const, mapped_type>.second)

However, if
pointer_to_value_type =
&(pair<key_type const, mapped_type>)
remains valid, then pointer_to_mapped_type will, as well.

I appreciate your help with this.


K. Frank
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top