List Question

This is a discussion on List Question within the C++ Programming forums, part of the General Programming Boards category; Sorry I would search the boards, but I'm having a really big connection problem with search feature at the moment. ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    4

    List Question

    Sorry I would search the boards, but I'm having a really big connection problem with search feature at the moment.


    My question is about <list>. I am using <list> to store a set of data in my program and right now I forsee the list ranging in size from nothing to about 30 items. I am erasing items from my list when that item in the list meets a certain requirement, but this is where my program is in big trouble because the item that gets erased may be the first item in the list, the middle or last and this is causing my for loop that cycles through the list to get messed up.

    Code:
    for(list<Data>::iterator i = mydata.begin(); i != mydata.end(); i++)
    {
    	if(i->Update())  //returns true when condition has been met to remove this item from list
    	{
                    list<Data>::iterator temp = i;
                    i++;
    		l_bullets.erase( temp );
    
    	}
    }
    I think the problem from this code will come when I am deleting the last item from my list ( because then i tries to increment up, but I just deleted the last item so it increments up into bad area). But if I change it to i-- I am just going to get the same problem if the item I am deleting is the first item in the list. I also run into the problem that if there is only one item in the list and it gets deleted it doesn't matter if it is i-- or i++ because both directions are bad. If anyone can understand what I mean and help me it would be very appreciated, thank you.
    Last edited by ConsulVortex; 01-14-2006 at 03:48 AM.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You don't have to worry. The element at list.end() doesn't exist. Its an element after the end of the list. So if you erase the last element in the list the iterater points to the element before list.end(). incrementing the iterator wil set it to list.end() and the for- loop will correctly terminate.

    but there is a much simpler solution. erase will return an iterator to the next element in the list
    so you can use

    Code:
         for ( list<int>::iterator i = mydata.begin(); i != mydata.end(); ++i )
            if ( i->Update() )
                  i = mydata.erase(i);
    Kurt

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Still incorrect: this code skips updating all elements that are directly after a delete one.
    Code:
    for(list<Data>::iterator i = mydata.begin(); i != mydata.end(); /* noop */) {
      if(i->Update()) {
        i = mydata.erase(i);
      } else {
        ++i;
      }
    }
    On a sidenote, always use prefix ++ to increment iterators, unless you really need postfix.

    At this point I'd also like to recommend Scott Meyers's "Effective STL", which covers this among many other problems.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by CornedBee
    Still incorrect: this code skips updating all elements that are directly after a delete one.
    Very true.
    Kurt

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. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 02:29 AM

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