deleting list objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dasod

    deleting list objects

    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::iterato r i = objects.begin() ; i != objects.end(); ++i)
    (*i)->print();
    }
    void remove_active_o bjects()
    {
    for (Tlist::iterato r i = objects.begin() ; i != objects.end(); )
    {
    if ((*i)->is_active())
    {
    Tlist::iterator temp = i;
    ++i;
    objects.erase(t emp);
    delete *temp;
    }
    else
    {
    ++i;
    }
    }
    }

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

    print_all ("Before:");
    remove_active_o bjects();
    print_all ("After:");

    for (Tlist::iterato r 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



  • John Harrison

    #2
    Re: deleting list objects


    "dasod" <NoSpam@mapSoN. invalid> wrote in message
    news:be3m8b$h51 $1@news1.songne t.fi...[color=blue]
    > 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::iterato r i = objects.begin() ; i != objects.end(); ++i)
    > (*i)->print();
    > }
    > void remove_active_o bjects()
    > {
    > for (Tlist::iterato r i = objects.begin() ; i != objects.end(); )
    > {
    > if ((*i)->is_active())
    > {
    > Tlist::iterator temp = i;
    > ++i;
    > objects.erase(t emp);
    > delete *temp;[/color]

    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


    Comment

    • dasod

      #3
      Re: deleting list objects


      "John Harrison" <john_andronicu s@hotmail.com> kirjoitti
      viestissä:be4c5 m$16j48$1@ID-196037.news.dfn cis.de...[color=blue]
      >
      > "dasod" <NoSpam@mapSoN. invalid> wrote in message
      > news:be3m8b$h51 $1@news1.songne t.fi...[color=green]
      > > if ((*i)->is_active())
      > > {
      > > Tlist::iterator temp = i;
      > > ++i;
      > > objects.erase(t emp);
      > > delete *temp;[/color]
      >
      > This is not correct, the iterator temp (which is just a copy of i) is[/color]
      no[color=blue]
      > longer valid after you've erased the item.
      >
      > if ((*i)->is_active())
      > {
      > delete *i;
      > i = objects.erase(i );
      > }
      > else
      > {
      > ++i;
      > }
      >[/color]

      Yes, it looks better now. Thanks a lot.


      Comment

      Working...