list erase without invalidating iterator

C

catphive.lists

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.
 
M

Mark P

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--);
 
D

David Harmon

On 1 Dec 2006 18:46:59 -0800 in comp.lang.c++, (e-mail address removed)
wrote,
Is there a way to call erase(iter) on a list without invalidating the
iterator?

No. The iterator points to the element you are erasing; afterwards the
element is gone. You can't have a valid iterator pointing to a gone
element.
Can I make a copy of an iterator and then move forward the
original without moving the copy?

Sure, but remember that erase() returns the next iterator value, which
is probably what you want to use.
 
J

Jim Langston

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.

iter = MyContainer.erase(whatever);

iter will automatcially get incremented to the next element. So you don't
want to increment it in your for loop. Algorithm I use is:

for ( MyContainerType::iterator it = MyContainer.begin(); it !=
MyContainer.end(); )
{
if ( (*it).somecondition() == somethingelse )
it = MyContainer.erase( it );
else
++it;
}
 
H

Howard

Mark P said:
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.

But the erase function returns an iterator you can use after the erase
(pointing to the next item), so just do:

iter = erase(iter);

-Howard
 
V

Victor Bazarov

Howard said:
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
 
H

Howard

Victor Bazarov said:
Howard said:
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.

Ah, ok.
Yes. It doesn't change the fact that 'erase(iter++)' is a valid way
to do it, though.

Cool! Thanks, Victor.
-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top