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. Advertisements

  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. Advertisements

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. Sandeep Grover

    delete on delete !

    Sandeep Grover, Jul 12, 2003, in forum: C++
    Replies:
    19
    Views:
    949
    Chris \( Val \)
    Jul 22, 2003
  2. Gandalf
    Replies:
    6
    Views:
    456
    Gandalf
    Oct 17, 2003
  3. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    623
    Pete Becker
    Mar 30, 2006
  4. Replies:
    3
    Views:
    400
    Default User
    Jul 9, 2007
  5. Replies:
    7
    Views:
    799
    Pete Becker
    Jan 25, 2008
  6. Replies:
    2
    Views:
    415
    Frank Birbacher
    May 23, 2008
  7. Patrick Put
    Replies:
    0
    Views:
    158
    Patrick Put
    Aug 25, 2008
  8. Sebastian Mach
    Replies:
    5
    Views:
    488
Loading...