STL iterator arithmetic

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Generic Usenet Account

    STL iterator arithmetic

    This posting is just for clarification of my understanding. It appears
    to me that only vector and deque iterators (i.e. random access
    iterators) allow "iterator arithmetic" operations (like iter+2, iter-1
    etc.). Kindly confirm.

    Thanks,
    Song

  • Pete Becker

    #2
    Re: STL iterator arithmetic

    Generic Usenet Account wrote:[color=blue]
    > This posting is just for clarification of my understanding. It appears
    > to me that only vector and deque iterators (i.e. random access
    > iterators) allow "iterator arithmetic" operations (like iter+2, iter-1
    > etc.). Kindly confirm.
    >[/color]

    Forward iterators support incrementing.
    Bidirectional iterators support incrementing and decrementing.
    Random access iterators support incrementing, decrementing, and the sort
    of arithmetic that you've mentioned.

    In the containers provided by the Standard C++ Library, the sequences
    managed by vector and deque have random access iterators. The sequences
    managed by list, set, multiset, map, and multimap have bidirectional
    iterators.

    In containers from other sources, check the documentation.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)

    Comment

    • Victor Bazarov

      #3
      Re: STL iterator arithmetic

      Generic Usenet Account wrote:[color=blue]
      > This posting is just for clarification of my understanding. It appears
      > to me that only vector and deque iterators (i.e. random access
      > iterators) allow "iterator arithmetic" operations (like iter+2, iter-1
      > etc.). Kindly confirm.[/color]

      Also, basic_string iterators are of random access variety.

      V

      Comment

      • Generic Usenet Account

        #4
        STL -- Deleting elements from a vector/set at a specified location

        I am not seeing the method definition for deleting elements at a
        specified position in a vector or deque. Is that the way it is
        supposed to be? Here's what I have come up with. Is there a better
        way around?

        Thanks,
        Song

        ////////////////////////////////////////////
        template<typena me T>
        void
        eraseAtPosition (vector<T>& coll, size_t posn, size_t numElem = 1)
        {
        if(posn+numElem > coll.size())
        {
        cerr << "Out of bounds exception\n";
        return;
        }

        coll.erase(coll .begin()+posn, coll.begin()+po sn+numElem);
        }

        Comment

        Working...