Re: STL Vector: Unexpected behavior
- From: "bob" <bobtabulous@xxxxxxxxxxx>
- Date: 25 Aug 2005 08:53:55 -0700
Hi Uli,
Thanks for the reply.
>Erasing an element from a container always invalidates the iterator used for
>the deletion, i.e. you can't use it afterwards, not even increment it to
>the next element. For a vector, you should expect it to invalidate every
>iterator of that container.
I have read this before but I dont really understand it :(
>Also, removing elements from the middle of a
>vector is a slow operation, you should use list<> instead, then you can use
>this simple algorithm:
>iterator it = the_list.begin();
>while(it!=the_list.end)
>{
> if(some_condition(*it))
> the_list.erase(it++);
> else
> ++it;
>}
Actually, the obj is always removed; it is not conditional. This also
means that I am removing elements from the top of the vector.
Thanks
.
- Follow-Ups:
- Re: STL Vector: Unexpected behavior
- From: Ulrich Eckhardt
- Re: STL Vector: Unexpected behavior
- From: Ken Alverson
- Re: STL Vector: Unexpected behavior
- From: Igor Tandetnik
- Re: STL Vector: Unexpected behavior
- References:
- STL Vector: Unexpected behavior
- From: bob
- Re: STL Vector: Unexpected behavior
- From: Ulrich Eckhardt
- STL Vector: Unexpected behavior
- Prev by Date: Re: STL Vector: Unexpected behavior
- Next by Date: Re: STL Vector: Unexpected behavior
- Previous by thread: Re: STL Vector: Unexpected behavior
- Next by thread: Re: STL Vector: Unexpected behavior
- Index(es):
Relevant Pages
|