Deleting an object while iterating

This is a discussion on Deleting an object while iterating within the C++ Programming forums, part of the General Programming Boards category; Hi, I wrote this but I am not sure if is correct. Code: for (vector< vector<Point> >::iterator track = tracks_.begin(); ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Question Deleting an object while iterating

    Hi,
    I wrote this but I am not sure if is correct.
    Code:
    for (vector< vector<Point> >::iterator track = tracks_.begin(); track != tracks_.end(); track++) {
    		if (track->empty()) { // if track is empty, remove it
    			tracks_.erase(track);
    			track++;
    		}else {   //if there are points, deque
    			track->erase(track->begin());
    		}
    	}
    I have a vector of vector of points (2 ints) which I call tracks (1 track is 1 vector of points)
    I want to check all the tracks and if they contain points then delete the first one otherwise delete the track. Is this correct?

    Thanks in advance.
    Mac OS 10.6 Snow Leopard : Darwin

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,801
    You are right to suspect that something is wrong. In order to avoid incrementing an invalidated iterator, you should write:
    Code:
    for (vector< vector<Point> >::iterator track = tracks_.begin(); track != tracks_.end();) {
        if (track->empty()) {
            track = tracks_.erase(track);
        } else { //if there are points, deque
            track->erase(track->begin());
            ++track;
        }
    }
    However, if the track has exactly one point, then you will end up with empty tracks that are left in the vector. If that is undesirable, I suggest:
    Code:
    for (vector< vector<Point> >::iterator track = tracks_.begin(), end = tracks_.end(); track != end; ++track) {
        if (!track->empty()) {
            track->erase(track->begin());
        }
    }
    
    tracks_.erase(std::remove(tracks_.begin(), tracks_.end(), vector<Point>()), tracks_.end());
    // or use std::remove_if with a predicate that calls the empty member function.
    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
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Wink Thanks ;)

    It worked
    I barely ask questions here, but somehow you are the one who always solves them Thanks a lot.
    Mac OS 10.6 Snow Leopard : Darwin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Texture Management in OpenGL
    By Brafil in forum Game Programming
    Replies: 13
    Last Post: 07-16-2009, 04:32 PM
  2. Telling a shared_ptr not to delete object?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2008, 04:26 AM
  3. ERRPR: Object reference not set to an instance of an object
    By blackhack in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2005, 05:27 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 01:56 PM

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