Re: iterator invalidation trouble

From: Gianni Mariani (gi2nospam_at_mariani.ws)
Date: 12/30/03


Date: 30 Dec 2003 00:39:25 EST

Howard Hinnant wrote:

>
> You could use a container that only invalidates the erased element
> (list, set, multiset, etc.) and then do something like:
>
> for (it = v.begin(); it != v.end();)
> f(*it++);
>
> This is effectively:
>
> for (it = v.begin(); it != v.end();)
> {
> It temp = it;
> ++it;
> f(*temp);
> }
>
> That is, you increment off of the iterator before it possibly becomes
> invalidated.

This is not a general solution. This assumes that the current object is
the one that gets removed, what if it is the next object instread, what
it there is a cascade effect and all objects in the container get removed ?

I'll give you an example. Consider a list (like this one) that has
"client contexts". Each client may cause a disconnect (destruct) of any
number of other clients on the list. Hence each time you traverse,
every iterator must remain valid. (this is a true-life example BTW).



Relevant Pages

  • Re: STL Vectors & Memory
    ... >> Do I have to create a destructer, ... >> the last element in the container, ... >> iterator and the Vector itself cant be modified, ... I understood that a const_iterator does not prevent a client from ...
    (comp.lang.cpp)
  • Re: STL Vector: Unexpected behavior
    ... >Erasing an element from a container always invalidates the iterator used for ... >iterator of that container. ... means that I am removing elements from the top of the vector. ...
    (microsoft.public.vc.stl)
  • Re: Iterators and containers STL
    ... >> intentionally I am trying to avoid inheriting from CList and CArray ... >> A) Client has to declare the container, ... >> instatiate the iterator by passing the container as an argument. ...
    (comp.lang.cpp)
  • Re: Iterators and containers STL
    ... > A) Client has to declare the container, ... > instatiate the iterator by passing the container as an argument. ... > iterator pattern and use STL at the same time... ...
    (comp.lang.cpp)
  • Re: ANN: SymbolicWeb v0.1 (quite alpha) w/ source code this time
    ... server model on its head and constructing as much of the widgets ... client-side, and your framework at ... may dump the majority of the processing off to the client and the server acts ... between displaying a container or hiding a container ...
    (comp.lang.lisp)

Loading