如何将任何容器的迭代器递增指定值
how to increment the iterator of any container by a specified value?
我在v.begin((+3或v.end((-1等方面遇到错误。为什么我们不能在迭代器上操作?
有两个列表l和m
l.assign(m.begin((+3,m.end((-1(;
这段代码有错误
[Error]与"operator-"不匹配(操作数类型为"std::list::迭代器{aka std::_list_iterator}"answers"int"(
迭代程序尽可能抽象;迭代器可以是一个指向数组的指针,也可以是流中的一个位置,当你递增它时,它会从stdin
中读取,任何东西。因此,标准算术运算符仅针对随机访问迭代器实现,因此它们不会将潜在的昂贵操作隐藏在简单语法后面。
为了将任何迭代器递增/递减任意数量,可以使用std::advance
:
std::advance(it, 3); //increment by three
std::advance(it, -1); //decrement once
C++11提供了std::next
和std::prev
,它们以一种更实用的方式实现这一点:
auto new_it = std::next(it,3); //increment by three
auto new_it = std::prev(it); //decrement once
相关文章:
- 使用std::multimap迭代器创建std::list
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++中带有List类的迭代器Segfault
- 如何在c++迭代器类型中包装std::chrono
- 集合上的输出迭代器:assign和increment迭代器
- Boost Spirit,获取迭代器内部语义动作
- 对于set上的循环-获取next元素迭代器
- C++具有特定值类型的任何迭代器
- std::lower_bound 不是专门用于红黑树迭代器的任何技术原因吗?
- tbb::concurrent_unordered_map::unsafe_erase 是否会使任何现有迭代器失效
- 适配器,用于将任何迭代器转换为随机访问迭代器
- 如何在不使用迭代器的情况下访问任何向量的第 i 个元素
- C++:在 faux-STL 类中使用任何迭代器类型
- 如何在不将代码放入头文件的情况下处理任何迭代器
- 如果没有for循环,我如何直接找到映射中指向两个不同键的任何两个迭代器之间的元素数量
- 输入迭代器跳过空格,任何防止这种跳过的方法
- 如何将任何容器的迭代器递增指定值
- 任何使用boost MPL或类似功能进行迭代器过滤的方法
- 从任何结构或元组按字符串形式的字段键获取迭代器
- 有没有任何方法可以验证STL容器的迭代器