473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
8 1769

"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
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

"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
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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dave | last post by:
Hello all, I'm pondering why the default underlying container for std::priority_queue<> is std::vector<>. It would seem that inserts are liable to happen anywhere, which would make std::list<>...
3
by: z. f. | last post by:
Hi, i'm using code in my aspx page. i have data binding where i use <%# Container.DataItem("DateStart") %> i also use code that makes a loop inside a regular <% %> block how can i pass...
4
by: Neil Zanella | last post by:
Hello, I would like to know what the difference is among the constructs <%= %> for evaluating an expression and displaying the evaluated result on the page and <%# %>. In particular I would like...
5
by: ad | last post by:
I find there are some tag like <%= %> and <%# > in my .aspx file. What is the diffreence between <%= %> and <%# %>
3
by: | last post by:
I have been researching articles on google on how to create a simple RSS feed that sucks <title><blurb><link><date> out of a sql server 2000 database via an aspx page. I know it has to be pushed...
1
by: ammar_fake | last post by:
Hello! I have an Oracle linked server connected through MSDAORA. Linked server queries work perfectly - the "openquery" ones as well as the 4-part-named ones. The problem I have is with...
20
by: martin-g | last post by:
Hi. Mostly I program in C++, and I'm not fluent in C# and .NET. In my last project I began to use LinkedList<and suddenly noticed that can't find a way to sort it. Does it mean I must implement...
56
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be...
7
by: Renzr | last post by:
I have a problem about the std::set<>iterator. After finding a term in the std::set<>, i want to know the distance from the current term to the begin(). But i have got a error. Please offer me...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.