Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple Iterator question

Reply
Thread Tools

Simple Iterator question

 
 
RishiD
Guest
Posts: n/a
 
      10-30-2006
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++;
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-30-2006
RishiD wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
RishiD
Guest
Posts: n/a
 
      10-30-2006

Victor Bazarov wrote:
> RishiD wrote:
> > 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
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


Haha sorry. My professor told me otherwise, thanks.

Sorry for wasting your time.

 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      10-30-2006
In article < .com>,
"RishiD" <> wrote:

> 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++ );
}

--
To send me email, put "sheltie" in the subject.
 
Reply With Quote
 
rep_movsd
Guest
Posts: n/a
 
      10-30-2006
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

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-30-2006
rep_movsd wrote:

> 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
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      10-30-2006
"rep_movsd" <> wrote:

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

--
To send me email, put "sheltie" in the subject.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List iterator assignment fails, assert iterator not dereferencable David Bilsby C++ 5 10-09-2007 02:05 PM
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57