Thread: How to delete item from list, while iterating through list?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217

    How to delete item from list, while iterating through list?

    How would i delete an item from a list<> class while im iterating through the list? I need it to first delete the item, than go to the next item without any problems.

    Code:
    for(list<ThrowObject>::iterator it = throwObjects.begin(); it != throwObjects.end(); ++it)
    {
        ThrowObject& throwObject = *it;
    
        //do stuff
    
        if(****)  //delete the item
        {
          ??
        }
    }

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by 39ster View Post
    How would i delete an item from a list<> class while im iterating through the list? I need it to first delete the item, than go to the next item without any problems.
    You have to obtain the next iterator value BEFORE erasing the element the iterator is pointing to. Generally:

    Code:
    for(iterator it = x.begin(), next; it != x.end(); it = next)
    {
        next = it;
        ++next;
        // do stuff, possibly deleting the element
    }

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Worked. Thanks.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The "erase" method returns the next iterator when removing:
    Code:
    std::list<T>::iterator i = List.begin();
    while(i != List.end())
    {
      if(ShouldIDeleteThis())
      {
        i = List.erase(i);
      }
      else
      {
        i++;
      }
    }
    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.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can also avoid the manual loop if you turn the if (...) contents into a unary function/function object and use list::remove_if
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  3. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  4. delete an element from a linked list
    By hinman in forum C Programming
    Replies: 6
    Last Post: 10-17-2007, 08:30 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM