Question about using erase() with lists (STL)

This is a discussion on Question about using erase() with lists (STL) within the C++ Programming forums, part of the General Programming Boards category; Hi, I want to remove one element pointed by iterator from a list. I read that when I use erase() ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    76

    Question about using erase() with lists (STL)

    Hi,
    I want to remove one element pointed by iterator from a list. I read that when I use erase() on such list with precise iterator, then it returns the iterator for the element after which I removed. But now my question: is it correct not to catch what erase returns?
    Example:
    Code:
    #include <cstdio>
    #include <list>
    
    using namespace std;
    
    int main()
    {
            list<int>       L;
            for(int i = 0; i < 5; i++)
                    L.push_back(i+1);
            for(list<int>::iterator i = L.begin(); i != L.end(); i++)
                    printf("%d\n", (*i));
            printf(".\n");
            for(list<int>::iterator i = L.begin(); i != L.end(); i++)
            {
                    if((*i)==3)
                            printf("Hola!\n");
                    if((*i)==2)
                    {
                            L.erase(i); // or i=L.erase(i);
                            printf("%d\n", (*i));
                    }
            }
            for(list<int>::iterator i = L.begin(); i != L.end(); i++)
                    printf("%d\n", (*i));
            return 0;
    }
    Becouse when there is
    L.erase(i);
    then printf() prints "2" but this "2" is not on the list yet... so something here is wired? But when I have i=L.erase(i); then printf() prints "3" (next element) what is correct - but then I have to do i-- not to omit one possition in the loop, right? (If I don't i-- then in the moment there will be i++ so element with "3" will be omitted in the loop - no "Hola" at the output.). Can anyone explain me that?
    Regards.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I usually do this:
    Code:
    i = List.begin();
    while(i != List.end())
    {
      if(ElementShouldBeErased())
      {
        i = List.erase(i);
      }
      else
      {
        i++;
      }
    }
    I dunno about erasing i then calling i++, but I'd guess it's NOT correct.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> is it correct not to catch what erase returns?

    It is legal, but it won't work as you expect. Magos' version is the common way to erase in a loop.

    Obviously this is because erase returns the next valid position after the erased element, and the for loop does i++ skipping that valid position. That while loop only increments the iterator when erase does not do it itself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intersection of two STL lists
    By misterMatt in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2009, 12:25 AM
  2. STL Linked Lists
    By stanlvw in forum C++ Programming
    Replies: 12
    Last Post: 04-11-2008, 01:33 AM
  3. Linked Lists Question
    By SlyMaelstrom in forum C++ Programming
    Replies: 12
    Last Post: 11-12-2005, 11:03 AM
  4. STL container question
    By PJYelton in forum C++ Programming
    Replies: 8
    Last Post: 12-16-2002, 07:40 PM
  5. question about STL
    By free2run in forum C++ Programming
    Replies: 2
    Last Post: 12-16-2002, 11:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21