Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > erasing from a list within a loop?

Reply
Thread Tools

erasing from a list within a loop?

 
 
Gernot Frisch
Guest
Posts: n/a
 
      07-29-2004
in a vector I do this:

for (int i=0; i<v.size(); ++i)
{
if (...) {v.erase(v.begin() + i); --i; continue;}
}


.... how to do this with a list?

for (it = l.begin(); it!=l.end(); ++it)
{
if(...) {MYLIST::iterator it2 = it; --it2; l.erase(it); continue;}
}

what if "it == l.begin()" ?? How else to do this?




--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com



 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-29-2004
Gernot Frisch wrote:
>
> in a vector I do this:
>
> for (int i=0; i<v.size(); ++i)
> {
> if (...) {v.erase(v.begin() + i); --i; continue;}
> }
>
> ... how to do this with a list?
>
> for (it = l.begin(); it!=l.end(); ++it)
> {
> if(...) {MYLIST::iterator it2 = it; --it2; l.erase(it); continue;}
> }
>
> what if "it == l.begin()" ?? How else to do this?
>


for( it = l.begin(); it != l.end; ) {
if(...)
it = l.erase(it);
else
++it;
}

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-29-2004

"Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Gernot Frisch wrote:
> >
> > in a vector I do this:
> >
> > for (int i=0; i<v.size(); ++i)
> > {
> > if (...) {v.erase(v.begin() + i); --i; continue;}
> > }
> >
> > ... how to do this with a list?
> >
> > for (it = l.begin(); it!=l.end(); ++it)
> > {
> > if(...) {MYLIST::iterator it2 = it; --it2; l.erase(it); continue;}
> > }
> >
> > what if "it == l.begin()" ?? How else to do this?
> >

>
> for( it = l.begin(); it != l.end; ) {
> if(...)
> it = l.erase(it);
> else
> ++it;
> }
>


With the added bonus that you can use exactly the same code for a
std::vector.

Another alternative is

for( it = l.begin(); it != l.end; ) {
if(...)
l.erase(it++);
else
++it;
}

and this will work for set and maps as well (though not for vectors).

john


 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      07-29-2004
On Thu, 29 Jul 2004 13:21:38 +0100, "John Harrison"
<(E-Mail Removed)> wrote:

>
>"Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>> Gernot Frisch wrote:
>> >
>> > in a vector I do this:
>> >
>> > for (int i=0; i<v.size(); ++i)
>> > {
>> > if (...) {v.erase(v.begin() + i); --i; continue;}
>> > }
>> >
>> > ... how to do this with a list?
>> >
>> > for (it = l.begin(); it!=l.end(); ++it)
>> > {
>> > if(...) {MYLIST::iterator it2 = it; --it2; l.erase(it); continue;}
>> > }
>> >
>> > what if "it == l.begin()" ?? How else to do this?
>> >

>>
>> for( it = l.begin(); it != l.end; ) {
>> if(...)
>> it = l.erase(it);
>> else
>> ++it;
>> }
>>

>
>With the added bonus that you can use exactly the same code for a
>std::vector.


That's a very inefficient algorithm though - fine for std::list, but
for std::vector you should use std::remove_if so that it's O(n).

Tom
 
Reply With Quote
 
Gernot Frisch
Guest
Posts: n/a
 
      07-29-2004
> >> for( it = l.begin(); it != l.end; ) {
> >> if(...)
> >> it = l.erase(it);
> >> else
> >> ++it;
> >> }
> >>

> >
> >With the added bonus that you can use exactly the same code for a
> >std::vector.

>
> That's a very inefficient algorithm though - fine for std::list, but
> for std::vector you should use std::remove_if so that it's O(n).
>
> Tom


<headscratch, pageflip...>
er... yes. I see... But then I'd have to provide a function for
testing if something would have to be removed. Not easily possible in
my problem, but great idea to remember for further problems. I have a
std::list btw. (I chose it for exaclty this reasons: having to
remove/insert objects at random position).

-Gernot


 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      08-01-2004
"Gernot Frisch" <(E-Mail Removed)> wrote:

> > >> for( it = l.begin(); it != l.end; ) {
> > >> if(...)
> > >> it = l.erase(it);
> > >> else
> > >> ++it;
> > >> }
> > >>
> > >
> > >With the added bonus that you can use exactly the same code for a
> > >std::vector.

> >
> > That's a very inefficient algorithm though - fine for std::list, but
> > for std::vector you should use std::remove_if so that it's O(n).
> >
> > Tom

>
> <headscratch, pageflip...>
> er... yes. I see... But then I'd have to provide a function for
> testing if something would have to be removed. Not easily possible in
> my problem, but great idea to remember for further problems. I have a
> std::list btw. (I chose it for exaclty this reasons: having to
> remove/insert objects at random position).


Please show us the code inside the if check, my guess is that it would
be quite easy to provide a function/functor for the testing.
 
Reply With Quote
 
Gernot Frisch
Guest
Posts: n/a
 
      08-02-2004

"Daniel T." <(E-Mail Removed)> schrieb im Newsbeitrag
news(E-Mail Removed)...
> "Gernot Frisch" <(E-Mail Removed)> wrote:
>
> > > >> for( it = l.begin(); it != l.end; ) {
> > > >> if(...)
> > > >> it = l.erase(it);
> > > >> else
> > > >> ++it;
> > > >> }
> > > >>
> > > >
> > > >With the added bonus that you can use exactly the same code for

a
> > > >std::vector.
> > >
> > > That's a very inefficient algorithm though - fine for std::list,

but
> > > for std::vector you should use std::remove_if so that it's O(n).
> > >
> > > Tom

> >
> > <headscratch, pageflip...>
> > er... yes. I see... But then I'd have to provide a function for
> > testing if something would have to be removed. Not easily possible

in
> > my problem, but great idea to remember for further problems. I

have a
> > std::list btw. (I chose it for exaclty this reasons: having to
> > remove/insert objects at random position).

>
> Please show us the code inside the if check, my guess is that it

would
> be quite easy to provide a function/functor for the testing.


Yes, sure, it's just that I'm too lazy or don't want to implement new
functions for style reasons. Sounds silly, but that's me...
-Gernot


 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      08-02-2004
On Mon, 2 Aug 2004 08:59:37 +0200, "Gernot Frisch" <(E-Mail Removed)>
wrote:

>> > <headscratch, pageflip...>
>> > er... yes. I see... But then I'd have to provide a function for
>> > testing if something would have to be removed. Not easily possible

>in
>> > my problem, but great idea to remember for further problems. I

>have a
>> > std::list btw. (I chose it for exaclty this reasons: having to
>> > remove/insert objects at random position).

>>
>> Please show us the code inside the if check, my guess is that it

>would
>> be quite easy to provide a function/functor for the testing.

>
>Yes, sure, it's just that I'm too lazy or don't want to implement new
>functions for style reasons. Sounds silly, but that's me...


In many cases you can do without a separate function. Have a look at
boost's Lambda library.

Tom
 
Reply With Quote
 
Gernot Frisch
Guest
Posts: n/a
 
      08-02-2004

"tom_usenet" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> On Mon, 2 Aug 2004 08:59:37 +0200, "Gernot Frisch" <(E-Mail Removed)>
> wrote:
>
> >> > <headscratch, pageflip...>
> >> > er... yes. I see... But then I'd have to provide a function for
> >> > testing if something would have to be removed. Not easily

possible
> >in
> >> > my problem, but great idea to remember for further problems. I

> >have a
> >> > std::list btw. (I chose it for exaclty this reasons: having to
> >> > remove/insert objects at random position).
> >>
> >> Please show us the code inside the if check, my guess is that it

> >would
> >> be quite easy to provide a function/functor for the testing.

> >
> >Yes, sure, it's just that I'm too lazy or don't want to implement

new
> >functions for style reasons. Sounds silly, but that's me...

>
> In many cases you can do without a separate function. Have a look at
> boost's Lambda library.
>
> Tom


Huh!? for_each(a.begin(), a.end(), cout << _1;}
How can you provide code as an argument? How does it work?

The boost libraries are the most sophisticated libraries I've ever
seen. I use the boost::spirit library - really a masterpiece of C++
coding. Even though, you'll need to find a good C++ compiler that
really can handle it.

-Gernot


 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      08-02-2004
"Gernot Frisch" <(E-Mail Removed)> wrote:

> > > <headscratch, pageflip...>
> > > er... yes. I see... But then I'd have to provide a function for
> > > testing if something would have to be removed. Not easily possible
> > > in
> > > my problem, but great idea to remember for further problems. I
> > > have a
> > > std::list btw. (I chose it for exaclty this reasons: having to
> > > remove/insert objects at random position).

> >
> > Please show us the code inside the if check, my guess is that it
> > would
> > be quite easy to provide a function/functor for the testing.

>
> Yes, sure, it's just that I'm too lazy or don't want to implement new
> functions for style reasons. Sounds silly, but that's me...


So, first you say it would not be easy to "provide a function for
testing if something would have to be removed", now you admit that it
would probably be easy but you don't want to do it "for style reasons".

It may be possible to create the test *without* implementing a new
function. Are you willing to show us the code inside the if check to see
if that is possible?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
erasing from stl list Krzysztof Poc C++ 10 01-22-2010 09:11 AM
Erasing from middle of a list problem Angus C++ 2 11-01-2008 11:06 AM
Crash in erasing element of a list. mahajan.vibhor@gmail.com C++ 13 07-11-2006 03:49 PM
Conditional erasing elements from list - in a loop Tescobar C++ 12 07-25-2005 03:14 AM
Re: Error Download file from TFTP after erasing Flash ( Cisco 2610) Rainer Temme Cisco 2 07-09-2003 02:17 AM



Advertisments