how to get iterator to a particular position of a vector

后端 未结 2 1634
臣服心动
臣服心动 2020-12-24 01:30

Suppose i have a

std::vector v
//and ...
for(int i =0;i<100;++i) 
 v.push_back(i);

now i want an iterator to, let\'s say 10t

2条回答
  •  半阙折子戏
    2020-12-24 02:11

    This will work with any random-access iterator, such as one from vector or deque:

    std::vector::iterator iter = v.begin() + 10;
    

    If you want a solution that will work for any type of iterator, use next:

    std::vector::iterator iter = std::next(v.begin(), 10);
    

    Or if you're not on a C++11 implementation, advance:

    std::vector::iterator iter = v.begin();
    std::advance(iter, 10);
    

提交回复
热议问题