简体   繁体   English

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

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

除了提前还有更多的负数吗?

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

  • 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)) 适用于前向迭代器或更好(或者如果给出负距离,则适用于双向迭代器))

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::next()就像std::advance一样使用负数,在这种情况下要求迭代器必须是双向的。 std::prev() would be more readable when the intent is specifically to move backwards. 当intent专门向后移动时, std::prev()会更具可读性。

std::advance 的std ::提前

The function advance() increments the position of an iterator passed as the argument. 函数advance()递增作为参数传递的迭代器的位置。 Thus, the function lets the iterator step forward (or backward) more than one element: 因此,该函数允许迭代器向前(或向后)多个元素:

#include <iterator>
void advance (InputIterator& pos, Dist n)
  • Lets the input iterator pos step n elements forward ( or backward ). 允许输入迭代器pos步骤n元素向前( 或向后 )。
  • For bidirectional and random-access iterators, n may be negative to step backward. 对于双向和随机访问迭代器,n可能为负向后退。
  • Dist is a template type. Dist是模板类型。 Normally, it must be an integral type because operations such as <, ++, --, and comparisons with 0 are called. 通常,它必须是整数类型,因为调用<,++, - 和与0的比较等操作。
  • 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). 请注意,advance()不会检查它是否越过序列的end()(它无法检查,因为迭代器通常不知道它们运行的​​容器)。 Thus, calling this function might result in undefined behavior because calling operator ++ for the end of a sequence is not defined. 因此,调用此函数可能会导致未定义的行为,因为未定义为序列末尾调用operator ++。

std::next (and std::prev new in C++11) std :: next (和C ++ 11中的std::prev new)

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

cite from The C++ Standard Library Second Edition The C++ Standard Library Second Edition引用

They're pretty much the same, except that std::next returns a copy and std::advance modifies its argument. 它们几乎相同,除了std::next返回一个副本并且std::advance修改它的参数。 Note that the standard requires std::next to behave like std::advance : 请注意,标准要求std::next的行为类似于std::advance

24.4.4 Iterator operations [iterator.operations] 24.4.4迭代器操作[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 2.要求:对于双向和随机访问迭代器,n应为负数
3. Effects: Increments (or decrements for negative n) iterator reference i by n. 3.效果:增量(或负n的减量)迭代器引用i由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; 6.效果:相当于advance(x, n); return x; advance(x, n); return x;

Note that both actually support negative values if the iterator is an input iterator. 请注意,如果迭代器是输入迭代器,则它们实际上都支持负值。 Also note that std::next requires the iterator to meet the conditions of an ForwardIterator, while std::advance only needs an Input Iterator (if you don't use negative distances). 另请注意, std::next要求迭代器满足ForwardIterator的条件,而std::advance只需要一个Input Iterator(如果不使用负距离)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM