Simple Iterator question

R

RishiD

Hi,

I am using an iterator on a list, I wanted to know is there a simple
way to erase() the current element the iterator is pointing to and move
the iterator forward one?

The only way I can think of doing this is making a temp iterator equal
to orig position, then erasing, and setting the original iterator to
temp iterator++

Basically asking if there is a way to do the code below in two steps
instead of three.

Thanks,

RishiD

// move private iterator forward and eliminate person
void movingForward()
{
list<Person>::iterator tempIter = privIter;
circle.erase(privIter);
privIter = tempIter++;
}
 
V

Victor Bazarov

RishiD said:
I am using an iterator on a list, I wanted to know is there a simple
way to erase() the current element the iterator is pointing to and
move the iterator forward one?
[..]

Is this a trick question? 'std::list::erase' has a return value
type, use it.

V
 
R

RishiD

Victor said:
RishiD said:
I am using an iterator on a list, I wanted to know is there a simple
way to erase() the current element the iterator is pointing to and
move the iterator forward one?
[..]

Is this a trick question? 'std::list::erase' has a return value
type, use it.

V

Haha sorry. My professor told me otherwise, thanks.

Sorry for wasting your time.
 
D

Daniel T.

"RishiD said:
Hi,

I am using an iterator on a list, I wanted to know is there a simple
way to erase() the current element the iterator is pointing to and move
the iterator forward one?

Yes, simply call erase() and assign the return value to the iterator
object passed in.
The only way I can think of doing this is making a temp iterator equal
to orig position, then erasing, and setting the original iterator to
temp iterator++

Basically asking if there is a way to do the code below in two steps
instead of three.

Thanks,

RishiD

// move private iterator forward and eliminate person
void movingForward()
{
list<Person>::iterator tempIter = privIter;
circle.erase(privIter);
privIter = tempIter++;
}

void movingForward()
{
privIter = circle.erase( privIter );
}

or since you are working with a list, you could:

void movingForward()
{
circle.erase( privIter++ );
}
 
R

rep_movsd

Hi

Why not

circle.erase(privIter++);

privIter++ increments the iterator and returns the previous value
voila....

It would work even if the container didnt return an iterator from
erase()


Regards
Vivek
 
K

Kai-Uwe Bux

rep_movsd said:
Hi

Why not

circle.erase(privIter++);

privIter++ increments the iterator and returns the previous value
voila....

It would work even if the container didnt return an iterator from
erase()

By container, you mean std::list: the idiom you propose does not work for
std::vector because of iterator invalidation. Using the return from erase()
provides the more robust code -- you could change the container to another
sequence type.


Best

Kai-Uwe Bux
 
D

Daniel T.

rep_movsd said:
Why not

circle.erase(privIter++);

It's not as general purpose because it doesn't work for vector and may
not work for deque depending on where in the container the iterator is.

When you have a choice between a method that works for all Sequences and
one that only works for some Sequences, always choose the more general
approach unless you information that the specific method is faster.
privIter++ increments the iterator and returns the previous value
voila....

It would work even if the container didnt return an iterator from
erase()

All Sequences must return an iterator from erase, they don't all have to
keep the iterator valid.
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top