Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initialize std::vector<T>::iterator with arbitrary element?

Reply
Thread Tools

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

 
 
Rune Allnor
Guest
Posts: n/a
 
      02-01-2010
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
 
Reply With Quote
 
 
 
 
Vladimir Jovic
Guest
Posts: n/a
 
      02-01-2010
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-lit...t.html#faq-5.8
 
Reply With Quote
 
 
 
 
Richard Herring
Guest
Posts: n/a
 
      02-01-2010
In message
<(E-Mail Removed)>,
Rune Allnor <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
Forcing ASP.NET processing of an arbitrary file MWells ASP .Net 2 01-11-2005 12:23 AM
calling an arbitrary function w/ arbitrary arguments Honestmath C++ 5 12-13-2004 06:18 AM
Math problem - converting between arbitrary bases in perl - help! ! aaa Perl 1 05-28-2004 01:42 AM
Reading from arbitrary web.config? Microsoft ASP .Net 1 08-28-2003 10:57 AM



Advertisments