By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,120 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,120 IT Pros & Developers. It's quick & easy.

Conditional erasing elements from list - in a loop

P: n/a
Over one year ago somebody asked here: how to
remove selected elements from list
in a loop?. The answer was as follows:
for( it = l.begin(); it != l.end; ) {
if(...)
it = l.erase(it);
else
++it;
}
I use STL list, and I do the same like this:
for( it = l.begin(); it != l.end; ++it ) {
if(...) l.erase(it--);
}
Is that OK? Or does it depend on details
of the list implementation ??
Has it to be guaranteed that the operator --
is called before invalidation of the itarator?

regards -
O.C.
Jul 23 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
"Tescobar" <ol**************@gmail.com> wrote in message
news:7a******************************@localhost.ta lkaboutprogramming.com...
Over one year ago somebody asked here: how to
remove selected elements from list
in a loop?. The answer was as follows:
for( it = l.begin(); it != l.end; ) {
if(...)
it = l.erase(it);
else
++it;
}
I use STL list, and I do the same like this:
for( it = l.begin(); it != l.end; ++it ) {
if(...) l.erase(it--);
}
Is that OK? Or does it depend on details
of the list implementation ??
Has it to be guaranteed that the operator --
is called before invalidation of the itarator?

'operator --' must be called before 'erase' is called, because the
result of 'operator --' is passed to 'erase'. So I do not see why this
would not be ok. Though the first version is clearer and at least as
fast as the second version.

regards
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #2

P: n/a
Tescobar wrote:
[..]
for( it = l.begin(); it != l.end; ++it ) {
I hope you meant

for( it = l.begin(); it != l.end(); ++it ) {
if(...) l.erase(it--);
}
Is that OK? [...]


No. If the first element is to be erased, you will make 'it' invalid
by decrementing it past the 'l.begin()'. Incrementing it again does
not necessarily bring it back to be valid -- undefined behaviour.

V
Jul 23 '05 #3

P: n/a
On 2005-07-21 13:01:04 -0400, "Tescobar" <ol**************@gmail.com> said:
Over one year ago somebody asked here: how to
remove selected elements from list
in a loop?. The answer was as follows:
for( it = l.begin(); it != l.end; ) {
if(...)
it = l.erase(it);
else
++it;
}
I use STL list, and I do the same like this:
for( it = l.begin(); it != l.end; ++it ) {
if(...) l.erase(it--);
}
Is that OK? Or does it depend on details
of the list implementation ??
Has it to be guaranteed that the operator --
is called before invalidation of the itarator?


Think about what happens when the very first item in the list is being
erased (you'll run off the beginning of the list). Additionally, if you
can make the condition of your if() into a predicate, then you could
reduce the whole thing down to:

l.remove_if(predicate);

--
Clark S. Cox, III
cl*******@gmail.com

Jul 23 '05 #4

P: n/a
Clark S. Cox III wrote:
On 2005-07-21 13:01:04 -0400, "Tescobar" <ol**************@gmail.com> said:
Over one year ago somebody asked here: how to
remove selected elements from list
in a loop?. The answer was as follows:
for( it = l.begin(); it != l.end; ) {
if(...)
it = l.erase(it);
else
++it;
}
I use STL list, and I do the same like this:
for( it = l.begin(); it != l.end; ++it ) {
if(...) l.erase(it--);
}
Is that OK? Or does it depend on details
of the list implementation ??
Has it to be guaranteed that the operator --
is called before invalidation of the itarator?

Think about what happens when the very first item in the list is being
erased (you'll run off the beginning of the list). Additionally, if you
can make the condition of your if() into a predicate, then you could
reduce the whole thing down to:

l.remove_if(predicate);


Don't you mean

l.erase(l.remove_if(predicate));

??

V
Jul 23 '05 #5

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:%I*******************@newsread1.mlpsca01.us.t o.verio.net...
Clark S. Cox III wrote:
l.remove_if(predicate);

Don't you mean

l.erase(l.remove_if(predicate));

??


remove_if returns void and already takes care of erasing.

regards
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #6

P: n/a
Jakob Bieling wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:%I*******************@newsread1.mlpsca01.us.t o.verio.net...

Clark S. Cox III wrote:


l.remove_if(predicate);


Don't you mean

l.erase(l.remove_if(predicate));

??

remove_if returns void and already takes care of erasing.


I must confuse it with std::remove_if ...

V
Jul 23 '05 #7

P: n/a
On 2005-07-21 14:21:39 -0400, Victor Bazarov <v.********@comAcast.net> said:
Clark S. Cox III wrote:
On 2005-07-21 13:01:04 -0400, "Tescobar" <ol**************@gmail.com> said:
for( it = l.begin(); it != l.end; ++it ) {
if(...) l.erase(it--);
}
Is that OK? Or does it depend on details
of the list implementation ??
Has it to be guaranteed that the operator --
is called before invalidation of the itarator?

Think about what happens when the very first item in the list is being
erased (you'll run off the beginning of the list). Additionally, if you
can make the condition of your if() into a predicate, then you could
reduce the whole thing down to:

l.remove_if(predicate);


Don't you mean

l.erase(l.remove_if(predicate));


Nope, I mean just what I said.

--
Clark S. Cox, III
cl*******@gmail.com

Jul 23 '05 #8

P: n/a
"Tescobar" <ol**************@gmail.com> wrote in message
news:7a******************************@localhost.ta lkaboutprogramming.com...
I use STL list, and I do the same like this:
for( it = l.begin(); it != l.end; ++it ) {
if(...) l.erase(it--);
}
Is that OK? Or does it depend on details
of the list implementation ??


It is not OK, because if it == l.begin(), then it-- causes undefined
behavior.
Jul 23 '05 #9

P: n/a

Tescobar wrote:
Over one year ago somebody asked here: how to
remove selected elements from list
in a loop?. The answer was as follows:
for( it = l.begin(); it != l.end; ) {
if(...)
it = l.erase(it);
else
++it;
}
I use STL list, and I do the same like this:
for( it = l.begin(); it != l.end; ++it ) {
if(...) l.erase(it--);
}
Is that OK? Or does it depend on details
of the list implementation ??
Has it to be guaranteed that the operator --
is called before invalidation of the itarator?


The post decrement operator will be applied before the call to erase,
but as others have pointed out there is an out-of-range problem with
the iterator. A slight change does correct this problem
for( it = l.begin(); it != l.end(); ++it ) {
if(...) it = l.erase(it);
}
But the code is still messier than it need be. Abstracting the loop
operation and hiding the specifics of its implementation, leads to more
compact and more likely correct code in many cases.

Assuming that a predicate (or a value) can serve as the test for
deletion, I would recommend this idiom for iterating and deleting items
from a container:
it.erase( std::remove_if( it.begin(), it.end(), ...), it.end());
Just insert the predicate or the value to be deleted in place of the
ellipsis.

Greg

Jul 23 '05 #10

P: n/a
"Greg" <gr****@pacbell.net> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
The post decrement operator will be applied before the call to erase,
but as others have pointed out there is an out-of-range problem with
the iterator. A slight change does correct this problem for( it = l.begin(); it != l.end(); ++it ) {
if(...) it = l.erase(it);
}


No it doesn't. Well, it does, but it introduces another problem, which
amounts to the same thing.

In this version, after you execute

it = l.erase(it);

you still increment "it". The result is that the code will skip the element
after each one erased.
Jul 23 '05 #11

P: n/a
my question was:
for( it = l.begin(); it != l.end(); ++it )
{
if(...) l.erase(it--);
}
Is that OK? [...]

Victor wrote:
No. If the first element is to be erased, you
will make 'it' invalid
by decrementing it past the 'l.begin()'.
Incrementing it again does
not necessarily bring it back to be valid -
undefined behaviour.


Are you sure, that this behaviour is undefined
by ANSI c++ ?
I use GNU compilers, where STL is implemented
by SGI. Here, as a matter of fact, list is
implemented as the circular structure, i.e.:

list<T> mylist;
list<T>::iterator p=mylist.end();
p++; assert(p==mylist.end());
p--; assert(p==mylist.end());
//now insert exactly one element:
mylist.push_front(something_of_type_T);
p=mylist.begin();
p--; assert(p==mylist.end());
p++; assert(p==mylist.begin());

is a valid code - none of the asserts fail.
In another words, one can traverse the same
list infinitely many times by repetitive
++ only (or -- only). During this, the
interator will be invalid only one time
each cycle - when passing its end() value.
I always thought, that such a behaviour
of list::iterator is implementation-independend.
Has anybody seen a counterexample, i.e. an
implementation which is still ANSI c++ compliant,
by does not obey "circularity" ?

Best regards from Poland :-)
O.C.

Jul 24 '05 #12

P: n/a


Tescobar wrote:
Over one year ago somebody asked here: how to
remove selected elements from list
in a loop?. The answer was as follows:
for( it = l.begin(); it != l.end; ) {
if(...)
it = l.erase(it);
else
++it;
}

I think more universal would be:
for( it = l.begin(); it != l.end; ) {
if(...)
l.erase(it++);
else
++it;
}

This should work with any STL container. Original would not work for
map and set for example.

Regards,
Vyacheslav

Jul 25 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.