Jump to content
  • Advertisement
Sign in to follow this  
silverphyre673

Cleanly removing elements from an std::vector

This topic is 4651 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'm having trouble figuring out a way to cleanly and efficiently remove elements from a vector. What I'm doing is iterating through a vector, and during the loop, elements may need to be removed, or they may not, and the vector is not sorted. I need to remove the unwanted elements from the middle of the vector, hopefully without stopping looping. When I tried vector.erase(iterator), the program threw a segfault, which I kind of expected :( If I need to do something like loop through the vector backwards and remove values that way, how would I do that? I found a bunch of other kind of iterators, like the reverse_iterator, but I don't understand how to use them. Any tips? Thanks. I would just like the easiest to understand (primarily), and most efficient (secondarily) way to do this.

Share this post


Link to post
Share on other sites
Advertisement
Yeah, the solution is a bit dirty-looking, but it works:

for(it = myVec.begin(); it != myVec.end();)
{
if(NeedsRemoval(*it))
it = erase(it);
else
++it;
}

The direction you iterate in doesn't matter, since the key issue is that the current iterator is invalidated no matter what.

Share this post


Link to post
Share on other sites
Since your elements are not sorted, how about something like this:

MyVec::iterator It (myVec.begin ()) ;
MyVec::size_type NElems = myVec.size () ;
MyVec::size_type Counter = 0 ;
while (Counter != NElems)
{
//If current element need to be removed
if (NeedsRemoval (*It))
{
(*It) = myVec.back () ; //Save the last element to the current
//removing element, to ensure there's no
//'hole' in the array
m_Vec.pop_back () ; //The last element is saved, we may remove
//it by now
if (Counter != (NElems - 1))
--NElems ; //NElems is now decreased by 1, be careful
//with this, if we don't have the if
//statement, !this is a bug!
}

++It ;
++Counter ;
}

You may remove duplicate elements in the same loop without invalidating the iterator also.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zipster

for(it = myVec.begin(); it != myVec.end();)
{
if(NeedsRemoval(*it))
it = erase(it);
else
++it;
}



#include <algorithm>

myVec.erase( std:remove_if( myVec.begin(),myVec.end(), &NeedsRemoval), myVec.end() );




EDIT: forgot a comma

[Edited by - rip-off on January 28, 2006 5:02:11 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off

#include <algorithm>

myVec.erase( std:remove_if( myVec.begin(),myVec.end(), &NeedsRemoval) myVec.end() );


We have a winner.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!