如何让迭代器加2?
谁能告诉我如何将迭代器增加 2?
iter++
可用 - 我必须执行 iter+2
吗? 我怎样才能实现这个目标?
Can anybody tell me how to increment the iterator by 2?
iter++
is available - do I have to do iter+2
? How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
std::advance( iter, 2 );
此方法适用于不是随机访问迭代器的迭代器,但仍然可以通过实现进行专门化,使其在与随机访问迭代器一起使用时效率不低于
iter += 2
。std::advance( iter, 2 );
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.http://www.cplusplus.com/reference/std/iterator/advance/
其中 n 在你的情况下是 2 。
该函数的优点在于,如果“it”是随机访问迭代器,则
使用快速操作(即向量<,,>::迭代器)。 否则它会渲染到
(即 list<..>::iterator)
http://www.cplusplus.com/reference/std/iterator/advance/
where n is 2 in your case.
The beauty of this function is, that If "it" is an random access iterator, the fast
operation is used (i.e. vector<,,>::iterator). Otherwise its rendered to
(i.e. list<..>::iterator)
如果您没有可修改的迭代器左值,或者需要获取给定迭代器的副本(保持原始迭代器不变),则 C++11 附带新的辅助函数 -
std::next
/std::prev
: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
:您可以使用“加法赋值”运算符
You could use the 'assignment by addition' operator
我们可以使用 std::advance 以及 std::next,但是两者之间是有区别的。
advance
修改其参数并且不返回任何内容。 因此它可以用作:next
返回迭代器的修改副本:We can use both std::advance as well as std::next, but there's a difference between the two.
advance
modifies its argument and returns nothing. So it can be used as:next
returns a modified copy of the iterator:如果您不知道容器中是否有足够的下一个元素,则需要在每个增量之间检查容器的末尾。 ++ 和 std::advance 都不会为你做这件事。
您甚至可以推出自己的绑定安全高级功能。
如果您确定不会超过末尾,那么 std::advance( iter, 2 ) 是最好的解决方案。
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.
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.
假设列表大小可能不是步骤的偶数倍,您必须防止溢出:
根据集合实现,距离计算可能非常慢。 下面是最佳的并且更具可读性。 闭包可以更改为实用程序模板,其中列表结束值由 const 引用传递:
如果没有循环:
Assuming list size may not be an even multiple of step you must guard against overflow:
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:
If there is no looping:
非常简单的答案:
长答案:
你真的应该习惯于编写
++iter
而不是iter++
。 后者必须返回旧值(的副本),该旧值与新值不同; 这需要时间和空间。请注意,前缀增量 (
++iter
) 接受左值并返回左值,而后缀增量 (iter++
) 接受左值并返回右值。The very simple answer:
The long answer:
You really should get used to writing
++iter
instead ofiter++
. The latter must return (a copy of) the old value, which is different from the new value; this takes time and space.Note that prefix increment (
++iter
) takes an lvalue and returns an lvalue, whereas postfix increment (iter++
) takes an lvalue and returns an rvalue.