another map question

This is a discussion on another map question within the C++ Programming forums, part of the General Programming Boards category; can you safely define a reference to a value stored in map, or can adding elements to a map alter ...

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839

    another map question

    can you safely define a reference to a value stored in map, or can adding elements to a map alter the addresses of already stored values?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by m37h0d View Post
    can you safely define a reference to a value stored in map, or can adding elements to a map alter the addresses of already stored values?
    The basic algorithm for map doesn't "need" to change the address of already stored elements, but it may do so anyways. I wouldn't rely on them having the same address in production code...

    If you actually want to do that, you may want to store a pointer (for example a std::tr1::shared_ptr), and copy the pointer into some other variable - obviously, map will not change where your user-data is from a passed in pointer - it will just store the pointer together with your key.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    As far as I know, none of the node-based containers ever reallocate a node, once it is inserted and hasn't been removed (because the container can be reordered by changing the links).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    Quote Originally Posted by m37h0d
    can you safely define a reference to a value stored in map, or can adding elements to a map alter the addresses of already stored values?
    To quote section 23.1.2 of the 2003 edition of the C++ standard:
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by matsp View Post
    The basic algorithm for map doesn't "need" to change the address of already stored elements, but it may do so anyways. I wouldn't rely on them having the same address in production code...

    If you actually want to do that, you may want to store a pointer (for example a std::tr1::shared_ptr), and copy the pointer into some other variable - obviously, map will not change where your user-data is from a passed in pointer - it will just store the pointer together with your key.

    --
    Mats
    that's what i was thinking - i couldn't see any reason why it would need to change the addresses, but i didn't want to bet on it without knowing for sure.

    i already opted to go for storing a pointer. it meant i no longer had a trivial destructor, but that's no big deal.

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by laserlight View Post
    To quote section 23.1.2 of the 2003 edition of the C++ standard:
    excellent. thank you. i suppose i should find where that standard is located and google it myself when i have STL questions.

    it is a bit more elegant when i dont' have to use the pointers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding a Map to a program
    By Shogun32 in forum C++ Programming
    Replies: 1
    Last Post: 05-04-2009, 10:42 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 01:14 PM
  3. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 09:32 AM
  4. Creating a map of objects with variable width and height
    By MrSparky in forum C++ Programming
    Replies: 6
    Last Post: 07-30-2007, 04:06 PM
  5. Keys of a map
    By nickname_changed in forum C++ Programming
    Replies: 4
    Last Post: 07-10-2003, 05:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21