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

Discussion in 'C++' started by James Aguilar, Mar 22, 2005.

  1. "Neil" <> wrote in message
    news:...
    > 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
     
    James Aguilar, Mar 22, 2005
    #1
    1. Advertising

  2. "Victor Bazarov" <> wrote in message
    news:BlZ%d.55267$01.us.to.verio.net...
    >
    > typename T::iterator it = ...


    Could you tell us why this is necessary?

    - JFA1
     
    James Aguilar, Mar 22, 2005
    #2
    1. Advertising

  3. James Aguilar

    Neil Guest

    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();
    }
     
    Neil, Mar 22, 2005
    #3
  4. "Victor Bazarov" <> wrote in message
    news:LNZ%d.55270$01.us.to.verio.net...
    > James Aguilar wrote:
    >> "Victor Bazarov" <> wrote in message
    >> news:BlZ%d.55267$01.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
     
    James Aguilar, Mar 22, 2005
    #4
  5. 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
     
    Victor Bazarov, Mar 22, 2005
    #5
  6. Malte Starostik, Mar 22, 2005
    #6
  7. James Aguilar

    Mark Guest

    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
    }
     
    Mark, Mar 22, 2005
    #7
  8. James Aguilar wrote:
    > "Victor Bazarov" <> wrote in message
    > news:BlZ%d.55267$01.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
     
    Victor Bazarov, Mar 22, 2005
    #8
  9. James Aguilar

    Kristo Guest

    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
     
    Kristo, Mar 22, 2005
    #9
    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. Tina Li
    Replies:
    0
    Views:
    326
    Tina Li
    Sep 18, 2003
  2. Anjan Bhowmik
    Replies:
    1
    Views:
    512
    Misbah Arefin
    Feb 14, 2008
  3. Clarendon

    Delete all items in the list

    Clarendon, Feb 26, 2009, in forum: Python
    Replies:
    21
    Views:
    625
    Terry Reedy
    Feb 28, 2009
  4. Jeremy
    Replies:
    3
    Views:
    422
  5. harry

    Delete all items from Drop Down?

    harry, Jun 16, 2004, in forum: Javascript
    Replies:
    7
    Views:
    98
    Grant Wagner
    Jun 17, 2004
Loading...

Share This Page