iterator invalidation trouble

From: Alexander Stippler (stip_at_mathematik.uni-ulm.de)
Date: 12/29/03


Date: Mon, 29 Dec 2003 18:22:31 +0100

Hi,

I've got trouble with some well known issue. Iterator invalidation. My
situation:

for (it=v.begin(); it!=v.end(); ++it) {
        f(*it);
}

Under some circumstances, f may alter the container by removing the current
item. Thus the iterator it gets invalid. My question is, if there is a way
to handle this specific case. The problem is that the for loop is user
code, and the user should not even notice that v changed, so I cannot
simply introduce a second iterator inside the for loop. The iterator it
should simply go on iterating with the following item, if *it is deleted.
*it is defininitely the only item which can be deleted by f.
I think this problem is rather general. Are there any threads online on
"robust iterators for c++"? Is there a trick for my situation?

regards,
        alex


Quantcast