Jump to content
  • Advertisement

Archived

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

IFooBar

deleting a vector element from within a vector loop

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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!
Click here to learn more

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 ] ::::

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!