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

deleting list objects

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"dasod" <No****@mapSoN.invalid> wrote in message
news:be**********@news1.songnet.fi...
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
Jul 19 '05 #2

P: n/a

"John Harrison" <jo*************@hotmail.com> kirjoitti
viestissä:be************@ID-196037.news.dfncis.de...

"dasod" <No****@mapSoN.invalid> wrote in message
news:be**********@news1.songnet.fi...
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.
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.