🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by Bowden Wise Thu, 16 Nov 1995 18:57
Does remove_if invalidate iterators?
#2598
Author: Bowden Wise
Date: Thu, 16 Nov 1995 18:57
34 lines
1551 bytes
I am using the remove_if () construct to remove a series of values
from a list<int>  ... I am doing a loop like this:

list<int>::const_iterator next (l.begin());
list<int>::iterator       last (l.begin());
while ( next != l.end() )
{

   // increment before remove_if 
   list<int>::const_iterator cur ( next++ );
   int val = *cur;

   if ( val*val > MAX )
      break;

   //
   last = remove_if (last, l.end(), bind2nd (myPred, val));  

}

However, I am finding that each time remove_if is called, it *skips* the next
item in the sequence.  So, does the remove_if mess up the semantics of my
iterator?  What should I do?

--------------------------------------------------------------------
G. Bowden Wise
Computer Science Dept, Rensselaer Polytechnic Inst, Troy, NY 12180
Email: wiseb@cs.rpi.edu         WWW: http://www.cs.rpi.edu/~wiseb/


[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Does remove_if invalidate iterators?
#2587
Author: J. Kanze
Date: Sat, 18 Nov 1995 03:15
48 lines
2495 bytes
Bowden Wise (wiseb@cs.rpi.edu) wrote:

|> I am using the remove_if () construct to remove a series of values
|> from a list<int>  ... I am doing a loop like this:

|> list<int>::const_iterator next (l.begin());
|> list<int>::iterator       last (l.begin());
|> while ( next != l.end() )
|> {

|>    // increment before remove_if 
|>    list<int>::const_iterator cur ( next++ );
|>    int val = *cur;

|>    if ( val*val > MAX )
|>       break;

|>    //
|>    last = remove_if (last, l.end(), bind2nd (myPred, val));  

Attention: if bind2nd(myPred,val)( *next ) is true, the element referred
to by next will be erased, invalidating it.

|> }

|> However, I am finding that each time remove_if is called, it *skips* the
|> next item in the sequence.  So, does the remove_if mess up the semantics
|> of my iterator?  What should I do?

Off hand, I'd say you need a list class with a more complicated
iterator; one that works even if you delete the element it refers to.
Although I suspect that there is a way to do this with the STL classes,
it doesn't occur to me off hand.

On the other hand, where *should* next point if the corresponding
element has been removed?  Although in my own list class, incrementation
works as you seem to expect it to, any attempt to delete the incremented
element will fail (with an assertion failure).  So you're really not
much better off.
-- 
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils, itudes et rialisations en logiciel orienti objet --
              -- A la recherche d'une activiti dans une region francophone
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads