Re: erase/delete for all containers

Discussion in 'C++' started by tom_usenet, Aug 22, 2003.

  1. tom_usenet

    tom_usenet Guest

    On Fri, 22 Aug 2003 11:52:30 GMT, "sks_cpp" <>
    wrote:

    >// C is a container of pointers
    >
    >template<typename Container>
    >
    >void deleteObjects(Container& C)
    >
    >{
    >
    > C::iterator i = C.begin();
    >
    > while( i != C.end() )
    >
    > {
    >
    > C::iterator j = i++;
    >
    > delete *j;
    >
    > C.erase(j);
    >
    > }
    >
    >}
    >
    >I read online somewhere that this works for list, set, and map but NOT
    >vector and deque. Is that correct?


    Yes. For vector, iterators after the erase are invalidated. For deque,
    all iterators are invalidated if the erase isn't at the start or end.

    Tom
    tom_usenet, Aug 22, 2003
    #1
    1. Advertising

  2. > > >
    > > >I read online somewhere that this works for list, set, and map but NOT
    > > >vector and deque. Is that correct?

    > >
    > > Yes. For vector, iterators after the erase are invalidated. For deque,
    > > all iterators are invalidated if the erase isn't at the start or end.

    >
    > So, how are you supposed do the above for vector and deque?
    >
    > Thanks,
    > Satish
    >


    I think Victor's final answer works for all containers, and its simpler and
    more efficient, amazing.

    john
    John Harrison, Aug 23, 2003
    #2
    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. Victor Bazarov

    Re: erase/delete for all containers

    Victor Bazarov, Aug 22, 2003, in forum: C++
    Replies:
    0
    Views:
    427
    Victor Bazarov
    Aug 22, 2003
  2. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    363
    Pete Becker
    Mar 30, 2006
  3. Replies:
    3
    Views:
    328
    Default User
    Jul 9, 2007
  4. Replies:
    7
    Views:
    553
    Pete Becker
    Jan 25, 2008
  5. Sebastian Mach
    Replies:
    5
    Views:
    311
Loading...

Share This Page