Initialize std::vector<T>::iterator with arbitrary element?

Discussion in 'C++' started by Rune Allnor, Feb 1, 2010.

  1. Rune Allnor

    Rune Allnor Guest

    Hi all.

    Suppose I have a vector v,

    std::vector<T> v;

    and corresponding iterator i

    std::vector<T>::iterator i;

    Now I want to initalize i to point to some
    arbitrary element v[n] in the vector.

    What is the best practice way to do this
    initialization? I have only seen initalizations
    to either v.begin() or v.end().

    One naive idea - which even compiles - is

    i = v.begin() + n;

    However, table 7.6 in Josuttis' "The C++ Standard Library"
    indicates there should be a way to index reative to
    the iterator, something like

    std::vector<T>::iterator j=i[n];

    If correct, I would expect that something like

    i = (v.begin())[n];

    also would work. Which it doesn't. So did I
    misunderstand something?

    Rune
    Rune Allnor, Feb 1, 2010
    #1
    1. Advertising

  2. Rune Allnor wrote:
    > Hi all.
    >
    > Suppose I have a vector v,
    >
    > std::vector<T> v;
    >
    > and corresponding iterator i
    >
    > std::vector<T>::iterator i;
    >
    > Now I want to initalize i to point to some
    > arbitrary element v[n] in the vector.
    >
    > What is the best practice way to do this
    > initialization? I have only seen initalizations
    > to either v.begin() or v.end().
    >
    > One naive idea - which even compiles - is
    >
    > i = v.begin() + n;
    >
    > However, table 7.6 in Josuttis' "The C++ Standard Library"
    > indicates there should be a way to index reative to
    > the iterator, something like
    >
    > std::vector<T>::iterator j=i[n];
    >
    > If correct, I would expect that something like
    >
    > i = (v.begin())[n];
    >
    > also would work. Which it doesn't. So did I
    > misunderstand something?


    Yes, read here :
    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8
    Vladimir Jovic, Feb 1, 2010
    #2
    1. Advertising

  3. In message
    <>,
    Rune Allnor <> writes
    >Hi all.
    >
    >Suppose I have a vector v,
    >
    > std::vector<T> v;
    >
    >and corresponding iterator i
    >
    > std::vector<T>::iterator i;
    >
    >Now I want to initalize i to point to some
    >arbitrary element v[n] in the vector.
    >
    >What is the best practice way to do this
    >initialization? I have only seen initalizations
    >to either v.begin() or v.end().
    >
    >One naive idea - which even compiles - is
    >
    >i = v.begin() + n;


    That's fine (and efficient) for a random-access iterator.
    >
    >However, table 7.6 in Josuttis' "The C++ Standard Library"
    >indicates there should be a way to index reative to
    >the iterator, something like
    >
    >std::vector<T>::iterator j=i[n];


    ITYM T t = i[n];
    >
    >If correct, I would expect that something like
    >
    >i = (v.begin())[n];
    >
    >also would work. Which it doesn't. So did I
    >misunderstand something?


    A level of indirection. i[n] returns a reference to an element of the
    vector, not an iterator.

    --
    Richard Herring
    Richard Herring, Feb 1, 2010
    #3
    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. Anonymous
    Replies:
    20
    Views:
    4,292
    Pete Becker
    Mar 30, 2005
  2. Replies:
    6
    Views:
    642
    Jim Langston
    Oct 30, 2005
  3. Jason Heyes
    Replies:
    8
    Views:
    720
    Andrew Koenig
    Jan 15, 2006
  4. Replies:
    8
    Views:
    1,914
    Csaba
    Feb 18, 2006
  5. Rune Allnor
    Replies:
    4
    Views:
    943
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page