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

Discussion in 'C++' started by K. Frank, May 27, 2013.

  1. K. Frank

    K. Frank Guest

    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. Frank, May 27, 2013
    #1
    1. Advertising

  2. K. Frank

    K. Frank Guest

    Hi Language Experts!

    On Sunday, May 26, 2013 9:55:17 PM UTC-4, K. Frank wrote:
    > 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
    K. Frank, May 30, 2013
    #2
    1. Advertising

  3. K. Frank

    Öö Tiib Guest

    On Thursday, 30 May 2013 19:02:55 UTC+3, K. Frank wrote:
    > On Sunday, May 26, 2013 9:55:17 PM UTC-4, K. Frank wrote:
    > > 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.


    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.
    Öö Tiib, May 30, 2013
    #3
  4. K. Frank

    K. Frank Guest

    Hello Öö!

    On May 30, 3:17 pm, Öö Tiib <> wrote:
    > On Thursday, 30 May 2013 19:02:55 UTC+3, K. Frank  wrote:
    > > On Sunday, May 26, 2013 9:55:17 PM UTC-4, K. Frank wrote:
    > > > 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.

    >
    > 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
    K. Frank, May 31, 2013
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kenneth Massey
    Replies:
    3
    Views:
    1,627
    Kenneth Massey
    Jul 21, 2004
  2. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    354
    Pete Becker
    Mar 30, 2006
  3. Replies:
    3
    Views:
    1,783
  4. Öö Tiib
    Replies:
    0
    Views:
    722
    Öö Tiib
    Jun 16, 2010
  5. Rahul Mathur
    Replies:
    1
    Views:
    141
    Victor Bazarov
    Feb 20, 2014
Loading...

Share This Page