Sign in to follow this  
lonewolff

Erasing an element in a vector

Recommended Posts

Hi Guys,

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

If I iterate through the vector like so...

[code]std::vector<ClientSocketData>::iterator it;
for(it=Client.begin();it<Client.end();it++)
{
// delete current item from list if certain condition is met
}[/code]
...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
[code]for (it = client.begin(); it != client.end(); ) {
if (condition == true)
it = client.erase(it);
else
++it;
}[/code]
[quote name='lonewolff' timestamp='1306410237' post='4815999']
Hi Guys,

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

If I iterate through the vector like so...

[code]std::vector<ClientSocketData>::iterator it;
for(it=Client.begin();it<Client.end();it++)
{
// delete current item from list if certain condition is met
}[/code]
...how can I delete / remove the item that is currently in use?

Your help is greatly appreciated. :cool:
[/quote]

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
[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
[/quote]

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:
[code]
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
}
[/code]

Share this post


Link to post
Share on other sites
[quote name='lpcstr' timestamp='1306488604' post='4816356']
[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
[/quote]

erase-remove of a std::vector uses the swap and pop method, correct?
[/quote]
it's like that
[code]
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;
}
[/code]
(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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this