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.