Howard wrote:
"Mark P" <us****@fall200 5REMOVE.fastmai lCAPS.fmwrote in message
news:O_******** *********@newss vr21.news.prodi gy.net...
>ca************@ gmail.com wrote:
>>Is there a way to call erase(iter) on a list without invalidating
the iterator? Can I make a copy of an iterator and then move
forward the original without moving the copy? I'm aware of the
existence of remove_if, but in the case I'm dealing with it would
be much more natural to use an iterator.
Probably what you want is one of:
erase( iter++);
erase( iter--);
That's not going to work is it? I think that incrementing in
invalidated iterator is just as invalid as using it again.
Nope. The iterator [still valid] is incremented just before the old
value (copied into a temporary object) is passed to the 'erase' function.
At the time of the function taking the necessary steps to remove the
element to which the iterator (passed into the function) points, the
iterator outside the function ('iter' variable) is pointing to the next
element. And that iterator is not going to be invalidated during the
erasure operation.
But the erase function returns an iterator you can use after the erase
(pointing to the next item), so just do:
iter = erase(iter);
Yes. It doesn't change the fact that 'erase(iter++)' is a valid way
to do it, though.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask