🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2570

Re: Modifying STL containers via an iterator

#2570
From: Anil A. Pal
Date: Wed, 15 Nov 1995 02:05
34 lines
1472 bytes
In article <u9ka52iwn4.fsf@yorick.cygnus.com>, jason@cygnus.com (Jason Merrill) writes:
|> This should work:
|> 
|> 	list< T >           myList ;
|> 	for ( list< T >::iterator i( myList.begin() ); i != myList.end(); )
|> 	{
|> 	    if ( someCondition( *i ) )
|> 	        myList.erase( i++ ) ;
|> 	}
|> 
|> Looks pretty similar, doesn't it?  Note that operator++ increments i
|> before it returns a value to be used in the erase.

Well, if someCondition doesn't hold, then i doesn't get incremented, and you
spend quite a long time in the loop :-)

I think you need to add an

	else {
	    ++i;
	}

to the if statement.

-- 
Anil A. Pal
pal@sgi.com	(415)-933-5279


---
[ 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. ]
Technical Details
Message-ID: <48bhlg$c69@fido.asd.sgi.com>
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
References: <47l8qe$9fk@bcarh8ab.bnr.ca> <9511122227.AA22118@eniac.glue.ch> <9511141139.AA01526@lts.sel.alcatel.de> <u9ka52iwn4.fsf@yorick.cygnus.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
From: pal@xanadu.engr.sgi.com (Anil A. Pal)
Newsgroups: comp.std.c++
Subject: Re: Modifying STL containers via an iterator
Date: 15 Nov 1995 02:05:05 GMT
Organization: Silicon Graphics, Inc.
Approved: matt@physics10.Berkeley.EDU (Matt Austern)
Message-ID: <48bhlg$c69@fido.asd.sgi.com>
References: <47l8qe$9fk@bcarh8ab.bnr.ca> <9511122227.AA22118@eniac.glue.ch>  <9511141139.AA01526@lts.sel.alcatel.de> <u9ka52iwn4.fsf@yorick.cygnus.com>
Reply-To: pal@sgihub.corp.sgi.com
NNTP-Posting-Host: physics10.berkeley.edu