How to delete the complete list
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());
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.
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 kingpinzsIterate through the container, and delete each element.
How would I do that?
std::list <Type *> Data; for(std::list <Type *> ::iterator iIndex = Data.begin(), iTail = Data.end(); iIndex != iTail; ++ iIndex) delete * iIndex; Data.clear();
Quote:Original post by kingpinzsI'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.
Is there another way to delete it with out using a pointer?
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
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement