Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List Iterator Remove()

Tags:

c++

list

stl

I have a list iterator that goes through a list and removes all the even numbers. I can use the list iterator to print out the numbers fine but I cannot use the list's remove() and pass in the dereferenced iterator.

I noticed that when the remove() statement is in effect, *itr gets corrupted? Can somebody explain this?

#include <iostream>
#include <list>

#define MAX 100

using namespace std;

int main()
{
    list<int> listA;
    list<int>::iterator itr;

    //create list of 0 to 100
    for(int i=0; i<=MAX; i++)
        listA.push_back(i);

    //remove even numbers
    for(itr = listA.begin(); itr != listA.end(); ++itr)
    {
        if ( *itr % 2 == 0 )
        {
            cout << *itr << endl;
            listA.remove(*itr);    //comment this line out and it will print properly
        }
    }
}
like image 684
Steve Avatar asked Jun 19 '09 04:06

Steve


People also ask

How do you remove from list iterator?

An element can be removed from a Collection using the Iterator method remove(). This method removes the current element in the Collection. If the remove() method is not preceded by the next() method, then the exception IllegalStateException is thrown.

Can we remove from list while iterating?

In Java 8, we can use the Collection#removeIf API to remove items from a List while iterating it.

What is difference between remove () method of collection and remove () method of iterator?

As per Sun , "Iterator. remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress."


2 Answers

There are a few issues with your code above. Firstly, the remove will invalidate any iterators that are pointing at the removed elements. You then go on to continue using the iterator. It is difficult to tell which element(s) remove would erase in the general case (although not in yours) since it can remove more than one.

Secondly, you are probably using the wrong method. Remove will iterate through all of the items in the list looking for any matching elements - this will be inefficient in your case because there is only one. It looks like you should use the erase method, you probably only want to erase the item at the position of the iterator. The good thing about erase is it returns an iterator which is at the next valid position. The idiomatic way to use it is something like this:

//remove even numbers
for(itr = listA.begin(); itr != listA.end();)
{
    if ( *itr % 2 == 0 )
    {
        cout << *itr << endl;
        itr=listA.erase(itr);
    }
    else
      ++itr;
}

Finally, you could also use remove_if to do the same as you are doing:

bool even(int i) { return i % 2 == 0; }

listA.remove_if(even);
like image 55
1800 INFORMATION Avatar answered Sep 18 '22 05:09

1800 INFORMATION


You can't use an iterator after you delete the element it referred to.

However, list iterators which refer to non-deleted items after a remove() should remain valid.

like image 34
Jonathan Maddison Avatar answered Sep 21 '22 05:09

Jonathan Maddison