$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
From: Michael Nicolella (boost_at_[hidden])
Date: 2006-07-11 03:20:22
vec.erase( std::remove_if( vec.begin(), vec.end(), some_condition_functor )
);
 
or:
 
for( std::vector<T>::iterator it = vec.begin(); it != vec.end() )
{
   if( some_condition )  it = vec.erase(it);
   else ++it;
}
 
 
the key here is that erase() returns the next valid iterator. "Stepping
back" like that isn't correct - after you call erase on that iterator, it's
invalidated. The first method above is usually preferred, the second works
but is less efficient.
 
FYI I coded this within the email, so there may be something wrong...
 
Mike
  _____  
From: bringiton bringiton [mailto:kneeride_at_[hidden]] 
Sent: Monday, July 10, 2006 11:56 PM
To: boost-users_at_[hidden]
Subject: [Boost-users] erase and iterators
is this common practise for using an interator with erase? what i wish to do
is interate through each item and delete the item if some condition is true.
when i delete, i need to move back a step so that i dont miss an item. 
for (std::vector<int>::iterator it = vect.begin(); it != vect.end(); it++) {
 if ((*it) == 5) { // some condition
  vect.erase(it);
  it--; //  step back
 }
}
or would it be best to use a reverse iterater? (or possible?)
for (std::vector<int>::reverse_iterator it2 = vect.rbegin(); it2 !=
vect.rend(); it2++) {
 if ((*it) == 5) {
  // i'm not actually sure how to erase with a reverse iterator???
  vect.erase(it2); 
 }
}