STL iterator arithmetic

Discussion in 'C++' started by Generic Usenet Account, Nov 15, 2005.

  1. 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
    Generic Usenet Account, Nov 15, 2005
    #1
    1. Advertising

  2. Generic Usenet Account

    Pete Becker Guest

    Generic Usenet Account wrote:
    > 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.
    >


    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)
    Pete Becker, Nov 15, 2005
    #2
    1. Advertising

  3. Generic Usenet Account wrote:
    > 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.


    Also, basic_string iterators are of random access variety.

    V
    Victor Bazarov, Nov 15, 2005
    #3
  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<typename 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()+posn+numElem);
    }
    Generic Usenet Account, Nov 16, 2005
    #4
    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. Marc Schellens

    Iterator/pointer arithmetic

    Marc Schellens, Dec 5, 2003, in forum: C++
    Replies:
    15
    Views:
    834
    tom_usenet
    Dec 8, 2003
  2. joshc
    Replies:
    5
    Views:
    539
    Keith Thompson
    Mar 31, 2005
  3. Christopher
    Replies:
    0
    Views:
    747
    Christopher
    Jan 13, 2009
  4. Bo Persson
    Replies:
    1
    Views:
    609
    Marcel Müller
    Oct 11, 2009
  5. Christopher
    Replies:
    9
    Views:
    575
    Marcel Müller
    Nov 6, 2012
Loading...

Share This Page