Iterator Invalidation

This is a discussion on Iterator Invalidation within the C++ Programming forums, part of the General Programming Boards category; I'm looking for info on what operations cause iterators to be invalidated - specifically, swap(). I have a couple std::list ...

  1. #1
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902

    Iterator Invalidation

    I'm looking for info on what operations cause iterators to be invalidated - specifically, swap(). I have a couple std::list structures that I'm going to call swap (in response to my class's swap()) on, and I have no idea if this invalidates iterators. The draft of the C++ standard I have makes no mention of this, which I find... unsettling. I would assume that for vectors/maps/lists/sets/etc. that swapping two containers shouldn't cause iterator invalidation... but is this guaranteed? Is there a good reference for these questions?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,113
    The draft of the C++ standard I have makes no mention of this, which I find... unsettling.
    You might be looking in the wrong place. The final version of the 2003 edition of the C++ Standard states:
    Quote Originally Posted by Section 23.1 Paragraph 10
    no swap() function invalidates any references, pointers, or iterators referring to the elements of the containers being swapped.
    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

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Could be I need to find a more recent draft... mine says "DRAFT: 2 Dec 1996", which is a bit before the one you mentioned. Although, awesome, no invalidation.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,113
    You have a copy of a draft of the 1998 version of the C++ Standard.

    Why not buy a PDF copy of the 2003 edition of the C++ Standard from the online ANSI store for 30 USD? You could wait for C++0x to come out, I suppose, but then it will take some time before compiler vendors properly support it anyway.
    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
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    There is a draft of the C++0x standard available for free, so if you didn't want to pay you could try that. There will be stuff in it that doesn't apply now, but at least it will be stuff that will work eventually.

Popular pages Recent additions subscribe to a feed

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