how to get iterator to a particular position of a vector(如何将迭代器获取到向量的特定位置)
本文介绍了如何将迭代器获取到向量的特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个
std::vector<int> v
//and ...
for(int i =0;i<100;++i)
v.push_back(i);
现在我想要一个迭代器,比如说向量的第 10 个元素.
now i want an iterator to, let's say 10th element of the vector.
不做下面的做法
std::vector<int>::iterator vi;
vi = v.begin();
for(int i = 0;i<10;i++)
++vi;
因为这会破坏为向量提供随机访问迭代器的优势.
as this will spoil the advantage of having random access iterator for a vector.
推荐答案
只需将 10 添加到迭代器即可.它们旨在感觉"像指针.
Just add 10 to the iterator. They are intended to "feel" like pointers.
这篇关于如何将迭代器获取到向量的特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何将迭代器获取到向量的特定位置


猜你喜欢
- STL 中有 dereference_iterator 吗? 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 静态初始化顺序失败 2022-01-01
- 近似搜索的工作原理 2021-01-01
- C++ 协变模板 2021-01-01