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); } }

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@gmail.com] Sent: Monday, July 10, 2006 11:56 PM To: boost-users@lists.boost.org 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); } }

Yes, I didn't like the step back. esp since if the first element was
deleted, the iterator would step out of vector memory (ie position -1).
Mike, I do like your remove_if example.
i am not familar with this approach, can you please give me an example of
defining a some_condition_functor? for example when the value == 5.
On 7/11/06, Michael Nicolella
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@gmail.com] *Sent:* Monday, July 10, 2006 11:56 PM *To:* boost-users@lists.boost.org *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); } }
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

# kneeride@gmail.com / 2006-07-11 17:31:23 +1000:
On 7/11/06, Michael Nicolella
wrote: vec.erase( std::remove_if( vec.begin(), vec.end(), some_condition_functor ) );
Mike, I do like your remove_if example.
i am not familar with this approach, can you please give me an example of defining a some_condition_functor? for example when the value == 5.
google is full of articles, tutorials and references for the stl. -- please trim unnecessary text, and avoid top posting. http://www.dtcc.edu/cs/rfc1855.html

On Tue, Jul 11, 2006 at 10:19:05AM +0000, Roman Neuhauser wrote:
# kneeride@gmail.com / 2006-07-11 17:31:23 +1000:
On 7/11/06, Michael Nicolella
wrote: vec.erase( std::remove_if( vec.begin(), vec.end(), some_condition_functor ) );
Mike, I do like your remove_if example.
i am not familar with this approach, can you please give me an example of defining a some_condition_functor? for example when the value == 5.
What about this? Let's assume the vector contains ints class condition_functor { public: bool operator()(int elem) { return elem==5 } };
participants (4)
-
Boris Breidenbach
-
bringiton bringiton
-
Michael Nicolella
-
Roman Neuhauser