Skip to main content
GameDev.net gamedev.net
🔒 Locked

std::list of pointers?

Started by ITGER Jan 19, 2005 at 6:13 PM 5 replies 1.1k views
Original Post
ITGER
ITGER
Hi, I've decided to give the standard library data structures a try, namely the one for linked lists. Suppose I have a list of pointers, which in turn point to objects on the heap. I want to iterate through the list once per iteration of the main program loop, and for each object that is marked "dead", remove it from the list. However, I'm having trouble deleting the object associated with the current pointer -and- removing the list element (the pointer itself). Here's an example of what I mean:

#include <iostream>
#include <list>

using namespace std;

class Thing
{
public:
    Thing() {cout<<"constructor"<<endl;}
    ~Thing() {cout<<"destructor"<<endl;}
};

int main()
{
    list <Thing *> thingList;

    Thing *pA = new Thing();
    Thing *pB = new Thing();
    Thing *pC = new Thing();
    Thing *pD = new Thing();


    thingList.push_back(pD);
    thingList.push_back(pC);
    thingList.push_back(pB);
    thingList.push_back(pA);

    list<Thing *>::iterator iter;  

	
    for (iter=thingList.begin(); iter != thingList.end(); iter++)
    {	
	delete *iter;
        thingList.erase(iter);  //this line crashes the program!
	cout<<thingList.size()<<endl;
    }

    return 0;
}

If I leave out that commented line (thingList.erase(iter)), the program dies. While deleting the object that the current pointer points to is great, shouldn't I also remove the list element containing the pointer itself also? Thanks
Promit
Promit
When you erase the iterator, the iterator becomes invalidated, so ++ on it doesn't work.

The easiest thing to do is probably to use a second iterator so that your loop iterator doesn't get destroyed.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
ITGER
ITGER
How would I go about using a second iterator for the loop?

Is the std::list effective for removing elements anywhere in the list? It has functions for popping from the front or the back, but I was hoping for a simple way to remove an element from possibly the middle of the list.
ITGER
ITGER
DrEvil that's not quite what I need to do. I want to iterate through my list, and delete -any- object that happens to be marked as "dead". (It could be at the front, middle, or end of the list) I didn't mean to mislead anyone with my example code, which just systematically traverses through and deletes everything in the list.

Besides, I think just doing myList.clear() will clean out the list.
Tron3k
Tron3k
Quote:
Original post by Promit
When you erase the iterator, the iterator becomes invalidated, so ++ on it doesn't work.
That's true. However, I think the erase function returns the next iterator, and then you can keep on iterating with that one.

Although, if you simply want to remove something if it is marked done, the STL remove_if algorithm sounds like the place to go. Just remember that it doesn't actually remove things from your list - it simply moves them to the end (since it can only work with iterators). You simply call erase as shown in the example on that page in order to actually erase them.
“[The clergy] believe that any portion of power confided to me, will be exerted in opposition to their schemes. And they believe rightly: for I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man” - Thomas Jefferson
ITGER
ITGER
Oh snap, you're right. erase() does return the next iterator, so I just did iter = thingList.erase(iter) and removed the iter++ from the for loop header. Now it works!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.