Thread: Need help to understand this statement

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    42

    Need help to understand this statement

    Could someone please help explain it to me what does the following statement means? It will be best if you could use some examples. Thanks

    An iterator can become invalid whenever a change is made to the underlying container.

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Code:
    vector<int>::iterator i = v.begin();
    
    v.push_back( 20 );
    
    // i can't be used anymore
    
    i = v.begin();
    
    // now i can be used again
    An iterator is like a pointer into the container, but if you change the container and it moves things around internally so that the pointer doesn't point to where the item is, the iterator is invalid.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Thanks

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The statement is, without context, too general, though. Among the properties of the various containers is a description of when, exactly, iterators can become invalid.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM