C++ 如何将迭代器增加 2?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1057529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to increment an iterator by 2?
提问by Cute
Can anybody tell me how to increment the iterator by 2?
谁能告诉我如何将迭代器增加 2?
iter++
is available - do I have to do iter+2
? How can I achieve this?
iter++
可用 - 我必须这样做iter+2
吗?我怎样才能做到这一点?
回答by CB Bailey
This method will work for iterators that are not random-access iterators but it can still be specialized by the implementation to be no less efficient than iter += 2
when used with random-access iterators.
此方法适用于不是随机访问迭代器的迭代器,但它仍然可以由实现专门化,使其效率不低于iter += 2
与随机访问迭代器一起使用时的效率。
回答by Maik Beckmann
http://www.cplusplus.com/reference/std/iterator/advance/
http://www.cplusplus.com/reference/std/iterator/advance/
std::advance(it,n);
where n is 2 in your case.
在您的情况下,n 为 2。
The beauty of this function is, that If "it" is an random access iterator, the fast
这个函数的美妙之处在于,如果“它”是一个随机访问迭代器,那么快速
it += n
operation is used (i.e. vector<,,>::iterator). Otherwise its rendered to
使用操作(即 vector<,,>::iterator)。否则它呈现为
for(int i = 0; i < n; i++)
++it;
(i.e. list<..>::iterator)
(即列表<..>::迭代器)
回答by Piotr Skotnicki
If you don't have a modifiable lvalue of an iterator, or it is desired to get a copy of a given iterator (leaving the original one unchanged), then C++11 comes with new helper functions - std::next
/ std::prev
:
如果您没有迭代器的可修改左值,或者希望获得给定迭代器的副本(保持原始迭代器不变),那么 C++11 带有新的辅助函数 - std::next
/ std::prev
:
std::next(iter, 2); // returns a copy of iter incremented by 2
std::next(std::begin(v), 2); // returns a copy of begin(v) incremented by 2
std::prev(iter, 2); // returns a copy of iter decremented by 2
回答by teabot
You could use the 'assignment by addition' operator
您可以使用“加法赋值”运算符
iter += 2;
回答by Jem
If you don't know wether you have enough next elements in your container or not, you need to check against the end of your container between each increment. Neither ++ nor std::advance will do it for you.
如果您不知道容器中是否有足够的下一个元素,则需要在每次增量之间检查容器的末尾。++ 和 std::advance 都不会为你做这件事。
if( ++iter == collection.end())
... // stop
if( ++iter == collection.end())
... // stop
You may even roll your own bound-secure advance function.
您甚至可以推出自己的绑定安全高级功能。
If you are sure that you will not go past the end, then std::advance( iter, 2 ) is the best solution.
如果你确定你不会走到最后,那么 std::advance( iter, 2 ) 是最好的解决方案。
回答by skpro19
We can use both std::advanceas well as std::next, but there's a difference between the two.
我们可以同时使用std::advance和std::next,但两者之间存在差异。
advance
modifies its argument and returns nothing. So it can be used as:
advance
修改其参数并且不返回任何内容。所以它可以用作:
vector<int> v;
v.push_back(1);
v.push_back(2);
auto itr = v.begin();
advance(itr, 1); //modifies the itr
cout << *itr<<endl //prints 2
next
returns a modified copy of the iterator:
next
返回迭代器的修改副本:
vector<int> v;
v.push_back(1);
v.push_back(2);
cout << *next(v.begin(), 1) << endl; //prints 2
回答by evoskuil
Assuming list size may not be an even multiple of step you must guard against overflow:
假设列表大小可能不是您必须防止溢出的步数的偶数倍:
static constexpr auto step = 2;
// Guard against invalid initial iterator.
if (!list.empty())
{
for (auto it = list.begin(); /*nothing here*/; std::advance(it, step))
{
// do stuff...
// Guard against advance past end of iterator.
if (std::distance(it, list.end()) > step)
break;
}
}
Depending on the collection implementation, the distance computation may be very slow. Below is optimal and more readable. The closure could be changed to a utility template with the list end value passed by const reference:
根据集合实现,距离计算可能非常慢。下面是最佳的,更具可读性。可以将闭包更改为具有由 const 引用传递的列表结束值的实用程序模板:
const auto advance = [&](list_type::iterator& it, size_t step)
{
for (size_t i = 0; it != list.end() && i < step; std::next(it), ++i);
};
static constexpr auto step = 2;
for (auto it = list.begin(); it != list.end(); advance(it, step))
{
// do stuff...
}
If there is no looping:
如果没有循环:
static constexpr auto step = 2;
auto it = list.begin();
if (step <= list.size())
{
std::advance(it, step);
}
回答by Jonas K?lker
The very simple answer:
非常简单的答案:
++++iter
The long answer:
长答案:
You really should get used to writing ++iter
instead of iter++
. The latter must return (a copy of) the old value, which is different from the new value; this takes time and space.
你真的应该习惯写作++iter
而不是iter++
. 后者必须返回(副本)旧值,与新值不同;这需要时间和空间。
Note that prefix increment (++iter
) takes an lvalue and returns an lvalue, whereas postfix increment (iter++
) takes an lvalue and returns an rvalue.
请注意,前缀 increment ( ++iter
) 接受一个左值并返回一个左值,而后缀 increment ( iter++
) 接受一个左值并返回一个右值。