转到基于范围的 for 循环中的下一个迭代器
Go to next iterator in range-based for loop
对于我的项目,我需要从循环中制作迭代器以转到容器中的下一项,执行一些操作,然后再次返回到相同的迭代器并继续,但是,由于某种原因,advance
和next
,然后使用prev
似乎都不起作用。那么我怎样才能获得下一个迭代器并返回到上一个迭代器呢?
我收到以下错误消息:
no matching function for call to 'next(int&)'
no type named 'difference_type' in 'struct std::iterator_traits<int>'
谢谢!
template<class T>
void insert_differences(T& container)
{
for(auto it : container){
// do some operations here
//advance(it,1);
it = next(it);
// do some operations here
//advance(it, -1);
it = prev(it);
}
}
基于范围的 for 循环对元素进行迭代。it
的名字在这里令人困惑;它不是迭代器而是元素,这就是为什么std::next
和std::prev
不使用它的原因。
在某个范围内执行 for 循环。
用作与传统 for 循环等效的更具可读性 对一系列值进行操作,例如容器中的所有元素。
你必须自己使用迭代器编写循环,比如
for(auto it = std::begin(container); it != std::end(container); it++){
// do some operations here
//advance(it,1);
it = next(it);
// do some operations here
//advance(it, -1);
it = prev(it);
}
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么我的for循环不能正确获取argv
- 在基于范围的for循环中使用结构化绑定声明
- 通过for循环使用用户输入填充列表
- 使用for循环检查数组中的重复项
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 正在使用for循环创建QScatterSerie
- Python中的for循环与C++有何不同
- 在更改for循环的第三部分后,未使用for循环结果
- 在 for 循环中查找问题时遇到困难
- 嵌套for循环C++的问题(初学者)
- 如何用for循环在c++中生成单词三角形
- 如何在for循环中包含两个索引值的测试条件
- 带有多个独立参数的C++For循环
- 为什么我的程序在for循环中k=0时返回垃圾值
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- C++-For循环未执行
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- C++ 无法在字符数组中使用 for 循环打印字母模式