[英]Go to next iterator in range-based for loop
对于我的项目,我需要使从循环到 go 的迭代器到容器中的下一个项目,执行一些操作,然后再次返回到同一个迭代器并继续,但是,由于某种原因既不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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.