Questions on safe use of std containers

Discussion in 'C++' started by Jayden Shui, Dec 20, 2011.

  1. Jayden Shui

    Jayden Shui Guest

    Hi All,

    As I know, it is not safe to reference the elements in a std::vector
    by pointers, since their address may change such as with the expansion
    of vector.

    My questions is that: how about std::set, map, and list? Are they safe
    or not by referencing their elements?

    Thanks a lot!

    Jayden
    Jayden Shui, Dec 20, 2011
    #1
    1. Advertising

  2. Jayden Shui <> wrote:
    > As I know, it is not safe to reference the elements in a std::vector
    > by pointers, since their address may change such as with the expansion
    > of vector.


    It's unsafe only if you add new elements to the vector while there are
    pointers/references pointing to existing elements. Otherwise it's safe
    (well, except for the case where you remove an element to which a
    pointer/reference was pointing, but that should be obvious).

    > My questions is that: how about std::set, map, and list? Are they safe
    > or not by referencing their elements?


    It's safe, except of course if you remove the element being pointed.
    Juha Nieminen, Dec 20, 2011
    #2
    1. Advertising

  3. Jayden Shui

    Goran Guest

    On Dec 20, 3:18 am, Jayden Shui <> wrote:
    > Hi All,
    >
    > As I know, it is not safe to reference the elements in a std::vector
    > by pointers, since their address may change such as with the expansion
    > of vector.
    >
    > My questions is that: how about std::set, map, and list? Are they safe
    > or not by referencing their elements?


    Yes.

    Standard C++ of 2003 says: "The insert 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" (that's for associative containers, 23.1.2/8).

    For a list, it says: "Does not affect the validity of iterators and
    references. If an exception is thrown there are no effects." (23.2.2.3
    list modifiers).

    Goran.
    Goran, Dec 20, 2011
    #3
  4. "Juha Nieminen" wrote in message
    news:4ef04194$0$4383$...
    >
    >Jayden Shui <> wrote:
    >> As I know, it is not safe to reference the elements in a std::vector
    >> by pointers, since their address may change such as with the expansion
    >> of vector.

    >
    > It's unsafe only if you add new elements to the vector while there are
    >pointers/references pointing to existing elements. Otherwise it's safe
    >(well, except for the case where you remove an element to which a
    >pointer/reference was pointing, but that should be obvious).


    My understanding is that also the use of "reserve" invalidates pointers and
    references.

    >
    >> My questions is that: how about std::set, map, and list? Are they safe
    >> or not by referencing their elements?

    >
    > It's safe, except of course if you remove the element being pointed.
    Fred Zwarts \(KVI\), Dec 20, 2011
    #4
  5. "Fred Zwarts \(KVI\)" <> wrote:
    > My understanding is that also the use of "reserve" invalidates pointers and
    > references.


    Only if the amount reserved exceeds the current capacity, but in that
    case yes, definitely.
    Juha Nieminen, Dec 20, 2011
    #5
  6. Jayden Shui

    Jorgen Grahn Guest

    On Tue, 2011-12-20, Goran wrote:
    > On Dec 20, 3:18 am, Jayden Shui <> wrote:
    >> Hi All,
    >>
    >> As I know, it is not safe to reference the elements in a std::vector
    >> by pointers, since their address may change such as with the expansion
    >> of vector.
    >>
    >> My questions is that: how about std::set, map, and list? Are they safe
    >> or not by referencing their elements?

    >
    > Yes.
    >
    > Standard C++ of 2003 says: "The insert 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" (that's for associative containers, 23.1.2/8).
    >
    > For a list, it says: "Does not affect the validity of iterators and
    > references. If an exception is thrown there are no effects." (23.2.2.3
    > list modifiers).


    It is also mentioning noting that these things are often called "the
    iterator invalidation rules" of the container, and that you /do/ need
    documentation which covers this for the containers you're using.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Dec 20, 2011
    #6
    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. Anton
    Replies:
    1
    Views:
    363
    Peter van Merkerk
    Aug 6, 2003
  2. Ross Boylan
    Replies:
    12
    Views:
    562
    Ross Boylan
    Feb 13, 2004
  3. Peter
    Replies:
    3
    Views:
    328
    Roland Pibinger
    Dec 12, 2006
  4. Replies:
    7
    Views:
    539
    Pete Becker
    Jan 25, 2008
  5. Sebastian Mach
    Replies:
    5
    Views:
    302
Loading...

Share This Page