By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
If I have a list<int*>, how should I delete the objects and clear the
list.
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();
}

Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"Neil" <Ne********@aol.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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
Jul 23 '05 #2

P: n/a
Neil wrote:
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.
If I have a list<int*>, how should I delete the objects and clear the
list.
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();


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
Jul 23 '05 #3

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Bl******************@newsread1.mlpsca01.us.to .verio.net...

typename T::iterator it = ...


Could you tell us why this is necessary?

- JFA1
Jul 23 '05 #4

P: n/a
James Aguilar schrieb:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Bl******************@newsread1.mlpsca01.us.to .verio.net...
typename T::iterator it = ...

Could you tell us why this is necessary?


I tried just a few hours ago:
http://groups.google.com/gr*********....net&lr=&hl=de

Cheers,
Malte
Jul 23 '05 #5

P: n/a
Neil wrote:
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.
If I have a list<int*>, how should I delete the objects and clear the
list.
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();
}


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
}

Jul 23 '05 #6

P: n/a
James Aguilar wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Bl******************@newsread1.mlpsca01.us.to .verio.net...
typename T::iterator it = ...

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
Jul 23 '05 #7

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:LN*******************@newsread1.mlpsca01.us.t o.verio.net...
James Aguilar wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Bl******************@newsread1.mlpsca01.us.to .verio.net...
typename T::iterator it = ...

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++.


Excellent, thanks.

- JFA1
Jul 23 '05 #8

P: n/a
On 3/22/2005 11:48 AM, Neil wrote:
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.
If I have a list<int*>, how should I delete the objects and clear the
list.
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();
}


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
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.