J
James Aguilar
Neil said:Hello,
Is there a standard or recommended way of handling deletion and removal
of objects in STL. I have loads of list of classes by pointer.
This is an example of a standard way to do it:
--- CODE ---
#include <list>
#include <algorithm>
using namespace std;
//Parameter: Const reference to a pointer -- promise not to change
//the pointer itself.
//Note: this could easily be made a template function.
void deleteIntPtr(int* const &i) {
delete i;
}
int main()
{
list<int*> test;
for (int i = 0; i < 100; ++i) {
test.push_back(new int);
}
//call the fn 'deleteIntPtr' on all elements in the array
for_each(test.begin(), test.end(), &deleteIntPtr);
return 0;
}
--- CODE ---
I also tried taking the address of operator delete, but that didn't work.
Remember, for_each cannot modify the contents of the container for its
iterators. I think.
- JFA1