Erasing an element in a vector

Started by
5 comments, last by Yang G 12 years, 10 months ago
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:
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:
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.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

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

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?
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
}

[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

This topic is closed to new replies.

Advertisement