Jump to content

  • Log In with Google      Sign In   
  • Create Account


Erasing an element in a vector


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 DarkRonin   Members   -  Reputation: 604

Like
0Likes
Like

Posted 26 May 2011 - 05:43 AM

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:

Sponsor:

#2 triangles   Members   -  Reputation: 106

Like
1Likes
Like

Posted 26 May 2011 - 05:45 AM

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:



#3 Rattrap   Members   -  Reputation: 1545

Like
3Likes
Like

Posted 26 May 2011 - 06:02 AM

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.

#4 Yang G   Members   -  Reputation: 105

Like
1Likes
Like

Posted 26 May 2011 - 09:39 PM

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

#5 lpcstr   Members   -  Reputation: 118

Like
0Likes
Like

Posted 27 May 2011 - 03:30 AM

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?

#6 rip-off   Moderators   -  Reputation: 7958

Like
1Likes
Like

Posted 27 May 2011 - 03:45 AM

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
}


#7 Yang G   Members   -  Reputation: 105

Like
0Likes
Like

Posted 28 May 2011 - 04:37 AM


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?

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS