deleting list objects

Discussion in 'C++' started by dasod, Jul 4, 2003.

  1. dasod

    dasod Guest

    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
     
    dasod, Jul 4, 2003
    #1
    1. Advertising

  2. "dasod" <> wrote in message
    news:be3m8b$h51$...
    > 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
     
    John Harrison, Jul 4, 2003
    #2
    1. Advertising

  3. dasod

    dasod Guest

    "John Harrison" <> kirjoitti
    viestissä:be4c5m$16j48$...
    >
    > "dasod" <> wrote in message
    > news:be3m8b$h51$...
    > > 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.
     
    dasod, Jul 4, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Crimarc

    Deleting sub-list from list

    Crimarc, Mar 7, 2005, in forum: C++
    Replies:
    3
    Views:
    382
    Karl Heinz Buchegger
    Mar 8, 2005
  2. KraftDiner
    Replies:
    2
    Views:
    349
    Mike Meyer
    Oct 20, 2005
  3. Harry Barker
    Replies:
    2
    Views:
    529
    Alf P. Steinbach
    Apr 19, 2006
  4. Sandy
    Replies:
    6
    Views:
    325
    Sandy
    Apr 22, 2010
  5. crea
    Replies:
    2
    Views:
    423
    Nobody
    Dec 28, 2012
Loading...

Share This Page