Try this:
vector<Type>::iterator nth = v.begin() + index;
way mentioned by @dirkgently ( v.begin() + index )
nice and fast for vectors
but std::advance
( v.begin(), index )
most generic way and for random access iterators works constant time too.
EDIT
differences in usage:
std::vector<>::iterator it = ( v.begin() + index );
or
std::vector<>::iterator it = v.begin();
std::advance( it, index );
added after @litb notes.
Also; auto it = std::next(v.begin(), index);
Update: Needs a C++11x compliant compiler
Or you can use std::advance
vector<int>::iterator i = L.begin();
advance(i, 2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With