Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > deleting list objects

Reply
Thread Tools

deleting list objects

 
 
dasod
Guest
Posts: n/a
 
      07-04-2003
I would like to know if my method to remove list objects is correct in
this small test program. It seems to me that there might be a simplier
way, but I'm afraid I don't know enough about list iterators and how
they are behaving in situations like this.

#include <iostream>
#include <list>

class Test;
typedef std::list< Test* > Tlist;
Tlist objects;

class Test
{
private:
bool Active;
int xx;
public:
Test(bool a, int x)
: Active(a), xx(x) {}
void print() { std::cout << Active << " \t" << xx << std::endl; }
bool is_active() { return Active; }
};

void print_all (const char *msg)
{
std::cout << msg << std::endl;
for (Tlist::iterator i = objects.begin(); i != objects.end(); ++i)
(*i)->print();
}
void remove_active_objects()
{
for (Tlist::iterator i = objects.begin(); i != objects.end(); )
{
if ((*i)->is_active())
{
Tlist::iterator temp = i;
++i;
objects.erase(temp);
delete *temp;
}
else
{
++i;
}
}
}

int main()
{
objects.push_back (new Test(true, 12));
objects.push_back (new Test(false, 345));
objects.push_back (new Test(false, 6789));
objects.push_back (new Test(true, 19283));

print_all ("Before:");
remove_active_objects();
print_all ("After:");

for (Tlist::iterator i = objects.begin(); i != objects.end(); ++i)
delete *i;

return 0;
}

---------------------------------
Output of this program should be:

Before:
1 12
0 345
0 6789
1 19283
After:
0 345
0 6789



 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-04-2003

"dasod" <(E-Mail Removed)> wrote in message
news:be3m8b$h51$(E-Mail Removed)...
> I would like to know if my method to remove list objects is correct in
> this small test program. It seems to me that there might be a simplier
> way, but I'm afraid I don't know enough about list iterators and how
> they are behaving in situations like this.
>
> #include <iostream>
> #include <list>
>
> class Test;
> typedef std::list< Test* > Tlist;
> Tlist objects;
>
> class Test
> {
> private:
> bool Active;
> int xx;
> public:
> Test(bool a, int x)
> : Active(a), xx(x) {}
> void print() { std::cout << Active << " \t" << xx << std::endl; }
> bool is_active() { return Active; }
> };
>
> void print_all (const char *msg)
> {
> std::cout << msg << std::endl;
> for (Tlist::iterator i = objects.begin(); i != objects.end(); ++i)
> (*i)->print();
> }
> void remove_active_objects()
> {
> for (Tlist::iterator i = objects.begin(); i != objects.end(); )
> {
> if ((*i)->is_active())
> {
> Tlist::iterator temp = i;
> ++i;
> objects.erase(temp);
> delete *temp;


This is not correct, the iterator temp (which is just a copy of i) is no
longer valid after you've erased the item.

if ((*i)->is_active())
{
delete *i;
i = objects.erase(i);
}
else
{
++i;
}

john


 
Reply With Quote
 
 
 
 
dasod
Guest
Posts: n/a
 
      07-04-2003

"John Harrison" <(E-Mail Removed)> kirjoitti
viestissä:be4c5m$16j48$(E-Mail Removed)...
>
> "dasod" <(E-Mail Removed)> wrote in message
> news:be3m8b$h51$(E-Mail Removed)...
> > if ((*i)->is_active())
> > {
> > Tlist::iterator temp = i;
> > ++i;
> > objects.erase(temp);
> > delete *temp;

>
> This is not correct, the iterator temp (which is just a copy of i) is

no
> longer valid after you've erased the item.
>
> if ((*i)->is_active())
> {
> delete *i;
> i = objects.erase(i);
> }
> else
> {
> ++i;
> }
>


Yes, it looks better now. Thanks a lot.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting data from the file without deleting the file first crea C++ 2 12-28-2012 11:50 PM
deleting objects present in a list Sandy Python 6 04-22-2010 06:43 PM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Deleting a File from Hardrive and Deleting a SubKey in Registry Harry Barker C++ 2 04-19-2006 09:34 AM
Having trouble deleting objects from a list... KraftDiner Python 2 10-20-2005 03:23 AM



Advertisments