How to delete and remove all items in a container<T*>

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
 
N

Neil

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.

e.g.
how should I delete the objects and clear the said:
From looking at this I see it can be done with a functor, my plain
iteration or by defining a template function.

my current idea is something like this - is it recommended ?:

template<typename T>
void clear_and_delete1(T& t) {
T::iterator it = t.begin(), end = t.end();
while(it != end)
delete (*(it++));
t.clear();
}

template<typename T>
void clear_and_delete2(T& t) {
T::iterator it = t.begin(), end = t.end();
while(it != end)
delete (**(it++));
t.clear();
}
 
J

James Aguilar

Victor Bazarov said:
'iterator' is a dependent name. Search Google Groups for "typename
dependent name" and you will get tons of links, I am sure. I thought
it's a textbook topic. If your compiler allows it, you should seek
an upgrade or use some kind of "disable extensions" or "strict" mode
of compiling if you want to make sure you're using Standard C++.

Excellent, thanks.

- JFA1
 
V

Victor Bazarov

Neil said:
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.

No standard way (except that a pointer obtained through 'new' needs to
be disposed of using 'delete', but you already know that). Recommended?
You are doing it right, I think. See notes below.
e.g.

iteration or by defining a template function.

my current idea is something like this - is it recommended ?:

template<typename T>
void clear_and_delete1(T& t) {
T::iterator it = t.begin(), end = t.end();

typename T::iterator it = ...
while(it != end)
delete (*(it++));
t.clear();
}

template<typename T>
void clear_and_delete2(T& t) {
T::iterator it = t.begin(), end = t.end();

typename T::iterator it = ...
while(it != end)
delete (**(it++));
t.clear();
}

BTW, shouldn't your functions be named "delete_and_clear" instead?

And, the second variation is needed only if you're storing pointers to
pointers, right? I've not encountered a need to have those yet (not the
pointers to pointers, but collections of them).

V
 
M

Mark

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.

e.g.

iteration or by defining a template function.

my current idea is something like this - is it recommended ?:

template<typename T>
void clear_and_delete1(T& t) {
T::iterator it = t.begin(), end = t.end();
while(it != end)
delete (*(it++));
t.clear();
}

template<typename T>
void clear_and_delete2(T& t) {
T::iterator it = t.begin(), end = t.end();
while(it != end)
delete (**(it++));
t.clear();
}

You can use STL list::erase to remove at each position on the list,
list::erase returns the iterator to the next valid position
after the erase. use delete at on the pointer

.....
list <myClass*>::iterator iter = test.begin();
while (iter != test.end())
{
delete(*iter); // deference of iter gets class ptr
iter = test.erase(iter); // clears list one at a time
// and point to next valid iter
}
 
V

Victor Bazarov

James said:
Could you tell us why this is necessary?

'iterator' is a dependent name. Search Google Groups for "typename
dependent name" and you will get tons of links, I am sure. I thought
it's a textbook topic. If your compiler allows it, you should seek
an upgrade or use some kind of "disable extensions" or "strict" mode
of compiling if you want to make sure you're using Standard C++.

V
 
K

Kristo

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.

e.g.

iteration or by defining a template function.

my current idea is something like this - is it recommended ?:

template<typename T>
void clear_and_delete1(T& t) {
T::iterator it = t.begin(), end = t.end();
while(it != end)
delete (*(it++));
t.clear();
}

template<typename T>
void clear_and_delete2(T& t) {
T::iterator it = t.begin(), end = t.end();
while(it != end)
delete (**(it++));
t.clear();
}

When you find yourself writing a loop over all container elements,
consider using for_each instead.

google: for_each delete

There's a multitude of info on the Internet for this. Poke around for a
while and see which solution suits you best.

Kristo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top