• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# deleting a vector element from within a vector loop

This topic is 5826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I remeber this being discussed sometime back, But I cant seem to find it. The problem is this.

for(std::vector<SomeThing*>::iterator i = myVect.begin(); i != myVect.end(); i++)
{
(*i)->DoSomething();
myVect.erase(i);
}

The app crashes. How do I do something like this. I have to delete i from inside the loop, but I dont know how to keep the loop valid, cause once I delete i, the loop breaks. thanks for any help.

:::: [ Triple Buffer V2.0 ] ::::

##### Share on other sites
Why don't you just :

    while(myVect.begin()!=myVect.end()) {*(myVect.begin())->DoSomething();myVect.erase(myVect.begin());}

or maybe:

       for(std::vector<SomeThing*>::iterator i = myVect.begin(); i != myVect.end(); i++) {	(*i)->DoSomething();if(NeedsToBeDeleted(i)) {myVect.erase(i);i--;}}

ToohrVyk
-------------
Extatica - a free 3d game engine
Available soon!

[edited by - ToohrVyk on February 9, 2003 11:32:01 AM]

##### Share on other sites
Inserting/removing elements from a vector, invalidates any iterators. I see you are using pointers in the vector (which you do not delete, any reason?), and I usually do like this:
for(std::vector<SomeThing*>::iterator i = myVect.begin(); i != myVect.end(); i++)    if(*i) delete *i;myVect.clear();
I think that is more efficient than to remove each and every element by themselves, as the vector might reallocate itself while it shrinks.

However, only let the elements be left unremoved in the loop if you are sure nothing will read from the vector before it is cleared. That is, beware if you're doing threaded stuff.

[edited by - CWizard on February 9, 2003 11:40:31 AM]

##### Share on other sites
Very easy to fix the problem. After delete a vector the current
iterator is invalid. But erase returns a valid iterator. So the only thing you must do is to write:
i = myVect.erase(i);

now it should work

##### Share on other sites
!!

Thanks all. But unfotuanetly all themethods crashed. So I put a few of your methods together and managed. namely like this

  for(std::vector<SomeThing*>::iterator i = myVect.begin(); i != myVect.end(); i++){	    (*i)->DoSomething();	    myVect.erase(i);    i = myVec.begin(); // imagine something this simple takes so long to figure out!}

CWizard: Yes I see your point. Although the way that this was designed does not allow for what you did. You see the DoSomething function is supposed to be a terminate function. myVec is a global vector that holds all the classes of type SomeThing that have been created. In the init function ''this'' is added to the vector. and in the terminate function, ''this'' is removed from that vector. So DoSomething deletes iterator ''i'' from within. Apparently adding the i = myVec.begin() solved the problem

thanks for everyones help.

:::: [ Triple Buffer V2.0 ] ::::

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015654
×