Jump to content
  • Advertisement
Sign in to follow this  
lonewolff

Erasing an element in a vector

This topic is 2607 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

Hi Guys,

I am pretty new to std::vectors and have a quick question.

If I iterate through the vector like so...

std::vector<ClientSocketData>::iterator it;
for(it=Client.begin();it<Client.end();it++)
{
// delete current item from list if certain condition is met
}

...how can I delete / remove the item that is currently in use?

Your help is greatly appreciated. :cool:

Share this post


Link to post
Share on other sites
Advertisement
for (it = client.begin(); it != client.end(); ) {
if (condition == true)
it = client.erase(it);
else
++it;
}


Hi Guys,

I am pretty new to std::vectors and have a quick question.

If I iterate through the vector like so...

std::vector<ClientSocketData>::iterator it;
for(it=Client.begin();it<Client.end();it++)
{
// delete current item from list if certain condition is met
}

...how can I delete / remove the item that is currently in use?

Your help is greatly appreciated. :cool:

Share this post


Link to post
Share on other sites
If the elements in the vector are not in any particular order, just copy the last element in the vector over the element you wish to delete, then pop off the last element.

This tends to be a more efficient way to remove elements from vectors. If you just call erase on an element in the middle of the vector, it has to move every element after that down a position, which could get expensive. The swap and pop version will only require a single copy.

Share this post


Link to post
Share on other sites
i think you'd better take a look at erase-remove idiom about vector.it's a efficient way to remove element in vector

Share this post


Link to post
Share on other sites

i think you'd better take a look at erase-remove idiom about vector.it's a efficient way to remove element in vector


erase-remove of a std::vector uses the swap and pop method, correct?

Share this post


Link to post
Share on other sites
No, but it will only copy an object to be retained at most once, whereas the algorithm triangles presented can move an object many times. It also maintains the original relative ordering, probably not important for client sockets but a desirable trait sometimes (e.g. swap-and-pop on a render list of sprites can result in some sprites suddenly drawing themselves on top of other sprites they had previously been "behind").

I believe the algorithm is something like this:

def remove_if(begin, end, predicate)
{
write = begin
for(read = begin ; read != end ; ++read)
{
if(!predicate(*read))
{
if(read != write)
{
*write = *read
}
++write
}
}
return write
}

Share this post


Link to post
Share on other sites

[quote name='Yang G' timestamp='1306467591' post='4816284']
i think you'd better take a look at erase-remove idiom about vector.it's a efficient way to remove element in vector


erase-remove of a std::vector uses the swap and pop method, correct?
[/quote]
it's like that

template < class ForwardIterator, class T >
ForwardIterator remove ( ForwardIterator first, ForwardIterator last, const T& value )
{
ForwardIterator result = first;
for ( ; first != last; ++first)
if (!(*first == value))
*result++ = *first;
return result;
}

(from c++ library).
Note:
remove doesn't delete the element in the vector instead of overwriting the element like the code said.
so we must use erase to delete these elements who should be removed like that vec.erase(remove(first, end, value), end).
you can see these subjects in book named Effective STL

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!