C++ std::advance 和 std::next 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15017065/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:54:04  来源:igfitidea点击:

What's the difference between std::advance and std::next?

c++c++11std

提问by Tavison

Is there more beyond advance takes negative numbers?

是否有更多超越提前需要负数?

回答by Benjamin Lindley

std::advance

std::advance

  • modifies its argument
  • returns nothing
  • works on input iterators or better (or bi-directional iterators if a negative distance is given)
  • 修改其参数
  • 什么都不返回
  • 适用于输入迭代器或更好(如果给出负距离,则为双向迭代器)

std::next

std::next

  • leaves its argument unmodified
  • returns a copy of the argument, advanced by the specified amount
  • works on forward iterators or better (or bi-directional iterators if a negative distance is given))
  • 保留其参数不变
  • 返回参数的副本,按指定的数量增加
  • 适用于前向迭代器或更好(或双向迭代器,如果给出负距离))

回答by Johnsyweb

Perhaps the biggest practical difference is that std::next()is only available in C++11.

也许最大的实际区别是std::next()仅在 C++11 中可用。

std::next()will advance by one by default, whereas std::advance()requires a distance.

std::next()默认情况下会前进一个,而std::advance()需要一个距离。

And then there are the return values:

然后是返回值:

std::next()takes negative numbers just like std::advance, and in that case requires that the iterator must be bidirectional. std::prev()would be more readable when the intent is specifically to move backwards.

std::next()就像 一样采用负数std::advance,在这种情况下要求迭代器必须是双向的。std::prev()当意图是专门向后移动时,会更具可读性。

回答by billz

std::advance

标准::提前

The function advance() increments the position of an iterator passed as the argument. Thus, the function lets the iterator step forward (or backward) more than one element:

函数 Advance() 增加作为参数传递的迭代器的位置。因此,该函数让迭代器前进(或后退)不止一个元素:

#include <iterator>
void advance (InputIterator& pos, Dist n)
  • Lets the input iterator pos step n elements forward (or backward).
  • For bidirectional and random-access iterators, n may be negative to step backward.
  • Dist is a template type. Normally, it must be an integral type because operations such as <, ++, --, and comparisons with 0 are called.
  • Note that advance() does not check whether it crosses the end() of a sequence (it can't check because iterators in general do not know the containers on which they operate). Thus, calling this function might result in undefined behavior because calling operator ++ for the end of a sequence is not defined.
  • 让输入迭代器 pos 向前(或向后)步进 n 个元素。
  • 对于双向和随机访问迭代器,n 可能为负以后退。
  • Dist 是一种模板类型。通常,它必须是整数类型,因为会调用 <、++、-- 和与 0 的比较等操作。
  • 请注意, Advance() 不会检查它是否穿过序列的 end() (它无法检查,因为迭代器通常不知道它们操作的容器)。因此,调用此函数可能会导致未定义的行为,因为未定义在序列末尾调用运算符 ++。

std::next(and std::prevnew in C++11)

std::next(以及std::prevC++11 中的新内容)

#include <iterator>
ForwardIterator next (ForwardIterator pos)
ForwardIterator next (ForwardIterator pos, Dist n)
  • Yields the position the forward iterator pos would have if moved forward 1 or n positions.
  • For bidirectional and random-access iterators, n may be negative to yield previous ositions.
  • Dist is type std::iterator_traits::difference_type.
  • Calls advance(pos,n) for an internal temporary object.
  • Note that next() does not check whether it crosses the end() of a sequence. Thus, it is up to the caller to ensure that the result is valid.
  • 如果向前移动 1 或 n 个位置,则产生前向迭代器 pos 将具有的位置。
  • 对于双向和随机访问迭代器,n 可能为负以产生先前的位置。
  • Dist 是 std::iterator_traits::difference_type 类型。
  • 为内部临时对象调用 Advance(pos,n)。
  • 请注意,next() 不会检查它是否穿过序列的 end()。因此,由调用者来确保结果有效。

cite from The C++ Standard Library Second Edition

引用自 The C++ Standard Library Second Edition

回答by Zeta

They're pretty much the same, except that std::nextreturns a copy and std::advancemodifies its argument. Note that the standard requires std::nextto behave like std::advance:

它们几乎相同,只是std::next返回一个副本并std::advance修改其参数。请注意,标准要求std::next行为如下std::advance

24.4.4 Iterator operations [iterator.operations]

template <class InputIterator, class Distance>
void advance(InputIterator& i [remark: reference], Distance n);

2. Requires: n shall be negative only for bidirectional and random access iterators
3. Effects: Increments (or decrements for negative n) iterator reference i by n.
[...]

template <class ForwardIterator>
ForwardIterator next(ForwardIterator x, [remark: copy]
     typename std::iterator_traits<ForwardIterator>::difference_type n = 1);

6. Effects: Equivalent to advance(x, n); return x;

24.4.4 迭代器操作 [iterator.operations]

template <class InputIterator, class Distance>
void advance(InputIterator& i [remark: reference], Distance n);

2. 要求:n 仅对于双向和随机访问迭代器应为负
3. 效果:通过 n 递增(或递减负 n)迭代器引用 i。
[...]

template <class ForwardIterator>
ForwardIterator next(ForwardIterator x, [remark: copy]
     typename std::iterator_traits<ForwardIterator>::difference_type n = 1);

6.效果:相当于 advance(x, n); return x;

Note that bothactually support negative values if the iterator is an input iterator. Also note that std::nextrequires the iterator to meet the conditions of an ForwardIterator, while std::advanceonly needs an Input Iterator (if you don't use negative distances).

请注意,如果迭代器是输入迭代器,则两者实际上都支持负值。另请注意,std::next需要迭代器满足 ForwardIterator 的条件,而std::advance只需要一个 Input Iterator(如果您不使用负距离)。