Simple Iterator question

Discussion in 'C++' started by RishiD, Oct 30, 2006.

  1. RishiD

    RishiD Guest

    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++;
    }
    RishiD, Oct 30, 2006
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Oct 30, 2006
    #2
    1. Advertising

  3. RishiD

    RishiD Guest

    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.
    RishiD, Oct 30, 2006
    #3
  4. RishiD

    Daniel T. Guest

    In article <>,
    "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.
    Daniel T., Oct 30, 2006
    #4
  5. RishiD

    rep_movsd Guest

    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
    rep_movsd, Oct 30, 2006
    #5
  6. RishiD

    Kai-Uwe Bux Guest

    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
    Kai-Uwe Bux, Oct 30, 2006
    #6
  7. RishiD

    Daniel T. Guest

    "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.
    Daniel T., Oct 30, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,404
  2. greg
    Replies:
    6
    Views:
    447
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    633
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,137
    Steven D'Aprano
    Apr 20, 2007
  5. David Bilsby
    Replies:
    5
    Views:
    2,027
    David Bilsby
    Oct 9, 2007
Loading...

Share This Page