|
-
Mar 2nd, 2006, 06:43 PM
#1
Thread Starter
PowerPoster
List: Erase element at iterator during iterations
I em having trouble with a list iteration.
I have to tranverse a list rather frequently, if an element is flagged for deletion then it must be removed.
for (list<int> iter = l.begin(); iter != l.end(); ++iter)
{
iter = l.erase(iter);
cout << (*iter) << endl;
}
This deletes every even number... since the first element is deleted, the 2nd is returns, then it is incremented by the for loop and deleted.
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Mar 3rd, 2006, 09:20 AM
#2
Lively Member
Re: List: Erase element at iterator during iterations
Increment "manually"
for (iter = l.begin(); iter!=l.end()
{
if (must erase)
iter = l.erase(iter);
else
++iter;
}
"bla, bla,... exists number M so for each n > M bla, bla..." Exists? Where is it? (Kronecker said...)
-
Mar 4th, 2006, 12:23 AM
#3
Frenzied Member
Re: List: Erase element at iterator during iterations
go through the list backwards
for (list<int> iter = l.end(); iter != l.begin(); --iter)
{
l.erase(iter);
cout << (*iter) << endl;
}
-
Mar 4th, 2006, 07:25 AM
#4
Re: List: Erase element at iterator during iterations
This will not work, when you call l.erase(iter) the iterator becomes invalid, any other action with that iterator is illegal.
The idiomatic C++ way to do what you want to do is using list::remove_if. An alternative is to increment the iterator before erasing it:
Code:
for (list<int>::iterator iter = l.begin(); iter != l.end(); )
{
list<int>::iterator to_erase = iter;
++iter;
if (i_want_to_erase_it) {
iter = l.erase(to_erase);
}
}
-
Mar 4th, 2006, 12:48 PM
#5
Thread Starter
PowerPoster
Re: List: Erase element at iterator during iterations
twanvl && bilm_ks methods worked fine with the expected results.
Now, twanvl your method works identically to bilm_ks, I cannot see why your method is more "correct".
If you go iter = l.erase(iter). The returned iterator is valid.
If you do not erase then ++iter.
So
while (iter != l.end())
{
if (erase) iter = l.erase(iter);
else ++iter;
}
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Mar 4th, 2006, 09:19 PM
#6
Re: List: Erase element at iterator during iterations
Indeed, bilm_ks's method is also recommended by Scott Meyers in Effective STL.
But twanvl is right, remove_if would be the typical way to do this.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|