Re: iterator invalidation trouble
From: Gianni Mariani (gi2nospam_at_mariani.ws)
Date: 12/30/03
- Next message: Jeff Schwab: "Re: iterator invalidation trouble"
- Previous message: John Tsiombikas (Nuclear / the Lab): "Re: Newbie Linking Error"
- In reply to: Howard Hinnant: "Re: iterator invalidation trouble"
- Next in thread: Jeff Schwab: "Re: iterator invalidation trouble"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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).
- Next message: Jeff Schwab: "Re: iterator invalidation trouble"
- Previous message: John Tsiombikas (Nuclear / the Lab): "Re: Newbie Linking Error"
- In reply to: Howard Hinnant: "Re: iterator invalidation trouble"
- Next in thread: Jeff Schwab: "Re: iterator invalidation trouble"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|