Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: erase/delete for all containers

Reply
Thread Tools

Re: erase/delete for all containers

 
 
tom_usenet
Guest
Posts: n/a
 
      08-22-2003
On Fri, 22 Aug 2003 11:52:30 GMT, "sks_cpp" <(E-Mail Removed)>
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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-23-2003
> > >
> > >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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Are sequence containers not a subset of general containers? Sebastian Mach C++ 5 10-06-2012 07:54 PM
![Oof Topix] All McCain, All Flip-Flop, All the Time Bucky Breeder Computer Support 7 09-27-2008 03:33 PM
SOAP call not returning containers all the time Patrick Put Ruby 0 08-25-2008 05:12 PM
Containers of iterators vs. containers of references clark.coleman@att.net C++ 7 01-25-2008 01:37 PM
Re: erase/delete for all containers Victor Bazarov C++ 0 08-22-2003 01:31 PM



Advertisments