15

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
        }
    }
}
Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
Steve
  • 11,831
  • 14
  • 51
  • 63

4 Answers4

47

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);
1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
3

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.

Eitan T
  • 32,660
  • 14
  • 72
  • 109
Jonathan Maddison
  • 1,067
  • 2
  • 11
  • 24
0

Could we use something like this:

container.erase(it++);

I tried on this example:

int main(){

 list<int>*a=new list<int>;
 a->push_back(1);
 a->push_back(2);
 a->push_back(3);

 list<int>::iterator I;

 I=a->begin(); ++I;

 a->erase(I++);
 cout<<*I<<endl;
}

and it displayed 3, as I wanted. Now I don't know if this is valid or one of those which "sometimes work and sometimes not".

EDIT: Maybe it is because of compiler. For example, compiler I am using (GNU gcc-g++) is treating lists (std::) as circular, ie if I increase iterator after list->end() it puts you at the beginning.

rimad
  • 1
  • This will work for `std::list` iterators because those iterators are only invalidated when the element they are pointing to is deleted. But this will not work for `std::vector` iterators because those iterators are invalidated when the element they are pointing to or any element before the one they are pointing to is deleted. – David Sep 24 '17 at 07:54
-2

Since iterators depend on the length of the structure remaining the same, most iterators do not allow a list to be changed while the iterator is in use. If you want to go through and change the list, you're going to have to use a loop independent of the iterator.

mnuzzo
  • 3,529
  • 4
  • 26
  • 29
  • 4
    I must point out that STL iterators do not have any dependancy on the length of the structure. Iterators quite often allow you to delete certain elements, for example vector iterators allow you to delete items past the iterator, and list iterators allow you to delete anything not pointed to by the iterator – 1800 INFORMATION Jun 19 '09 at 04:23