Thread: Iterator Invalidation

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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