Simple Iterator question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RishiD

    #1

    Simple Iterator question

    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>::i terator tempIter = privIter;
    circle.erase(pr ivIter);
    privIter = tempIter++;
    }

  • Victor Bazarov

    #2
    Re: Simple Iterator question

    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::era se' 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


    Comment

    • RishiD

      #3
      Re: Simple Iterator question


      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::era se' 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.

      Comment

      • Daniel T.

        #4
        Re: Simple Iterator question

        In article <1162170693.977 775.159550@b28g 2000cwb.googleg roups.com>,
        "RishiD" <rishid@gmail.c omwrote:
        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>::i terator tempIter = privIter;
        circle.erase(pr ivIter);
        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.

        Comment

        • rep_movsd

          #5
          Re: Simple Iterator question

          Hi

          Why not

          circle.erase(pr ivIter++);

          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

          Comment

          • Kai-Uwe Bux

            #6
            Re: Simple Iterator question

            rep_movsd wrote:
            Hi
            >
            Why not
            >
            circle.erase(pr ivIter++);
            >
            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

            Comment

            • Daniel T.

              #7
              Re: Simple Iterator question

              "rep_movsd" <rep.movsd@gmai l.comwrote:
              Why not
              >
              circle.erase(pr ivIter++);
              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.

              Comment

              Working...