Re: STL Vector: Unexpected behavior



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

.



Relevant Pages

  • Re: iterator invalidation trouble
    ... > You could use a container that only invalidates the erased element ... it there is a cascade effect and all objects in the container get removed? ... Each client may cause a disconnect of any ... every iterator must remain valid. ...
    (comp.lang.cpp)
  • Call for suggestions: Declaring data entry forms using Python classes
    ... For the purposes of GUI description, ... the renderer has to iterate over its fields. ... # instantiated as objects inside the container classes ... # I'm trying several versions of the iterator interface, ...
    (comp.lang.python)
  • Re: Maintance of c++ code
    ... because templates are not a method of implementing ... container, some only work with particular containers. ... inverting itself into an iterator of the opposite direction. ... explicit range(int end); ...
    (comp.object)
  • Re: STL vector help needed.
    ... places.beginthat returns an iterator to the first element of places. ... Because a string is a kind of container, ... think that you are quite some way from writing real code. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: [VS2008 SP1][std::vector] _CRT_DEBUGGER_HOOK crash in Release mode only -- [SOLVED by wo
    ... INT iText; ... Even if I use the same iterator code for the vector ... std::list container handles better? ... typedef vectorcontainer; ...
    (microsoft.public.vc.language)