How to delete the complete list

Started by
21 comments, last by kingpinzs 17 years, 2 months ago
If DataList2 is the std::list wont the fallowing code delete all of it? Because I am trying and it does not seam to be doing the trick. DataList2.erase (DataList2.begin(), DataList2.end());
Advertisement
It should. However, use the member function clear to clear the entire list instead of using the function taking an arbitrary iterator range.
As above, use clear().

Note that if the type contained with the list is a pointer, then delete will not be called on the pointers. The list cleans up what it allocates; you must clean up what you allocate.
Quote:Original post by jpetrie

Note that if the type contained with the list is a pointer, then delete will not be called on the pointers. The list cleans up what it allocates; you must clean up what you allocate.


How would I do that?

Quote:Original post by kingpinzs
How would I do that?
Iterate through the container, and delete each element.

	std::list <Type *> Data;	for(std::list <Type *> ::iterator iIndex = Data.begin(), iTail = Data.end(); iIndex != iTail; ++ iIndex)		delete * iIndex;	Data.clear();
Is there another way to delete it with out using a pointer?
Quote:Original post by kingpinzs
Is there another way to delete it with out using a pointer?
I'm not sure I understand what you mean. You only have to manually do what's outlined above if your container stores pointers in the first place.
I belive that this delete * iIndex; means a pointer to iIndex but I am not sure though. I tryed just delete iIndex it gave me error
type `struct std::_List_iterator<int>' argument given to `delete', expected pointer
You need to dereference the iterator to properly delete your data. In other words, "delete * iIndex" is what you should be using.
in this function

void Add(POINT pos,RECT Src)
{

paddle.Pos=pos;

paddle.Src=Src;

Sprites.push_back(paddle);

}

how could I remove pos and src instead of adding it?

This topic is closed to new replies.

Advertisement