What is the difference between std :: advance and std :: next?

Are there more than advance deviations that accept negative numbers?

+42
c ++ c ++ 11 std
Feb 22 '13 at 4:50
source share
4 answers

std::advance

  • changes its argument
  • returns nothing
  • works on input iterators or better (or bidirectional iterators if negative distance is given)

std::next

  • leaves its argument unmodified
  • returns a copy of the argument extended by the specified amount
  • works on iterators forward or better (or bidirectional iterators if negative distance is given))
+62
Feb 22 '13 at 4:54
source share

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

std::next() will advance one at a time by default, while std::advance() need a distance.

And then there are the return values:

std::next() accepts negative numbers just like std::advance , in which case it requires the iterator to be bidirectional. std::prev() will be more readable when the intent intentionally moves backward.

+13
Feb 22 '13 at 4:54
source share

stand :: advance

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

 #include <iterator> void advance (InputIterator& pos, Dist n) 
  • Allows you to enter an input iterator step n elements forward ( or backward ).
  • For bidirectional and random access iterators, n can be negative to go backward.
  • Dist - type of template. Usually it should be an integral type, since operations such as <, ++, - and comparisons with 0 are called.
  • Note that advance () does not check if it crosses the end () of the sequence (it cannot check, because iterators do not know at all the containers on which they work). Thus, a call to this function can lead to undefined behavior, because a call to the ++ operator to complete the sequence is undefined.

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

 #include <iterator> ForwardIterator next (ForwardIterator pos) ForwardIterator next (ForwardIterator pos, Dist n) 
  • Sets the position that the front iterator pos should have if it moved forward 1 or n positions.
  • For bidirectional and random access iterators, n can be negative to get previous ositions.
  • Dist is the type std :: iterator_traits :: difference_type.
  • A forward call (pos, n) for an internal temporary object.
  • Note that next () does not check if it intersects the end () of the sequence. Thus, you must ensure that the result is valid before the caller.

specify The C++ Standard Library Second Edition

+5
Feb 22 '13 at 5:03
source share

They are almost the same, except that std::next returns a copy, and std::advance changes its argument. Note that the standard requires std::next to behave like std::advance :

24.4.4 iterator operations [iterator.operations]

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

2. Required: n must be negative only for bidirectional and random access iterators
3. Effects: Increments (or decrements for negative n) of the iterator i reference to 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; advance(x, n); return x;

Note that both support negative values ​​if the iterator is an input iterator. Also note that std::next requires the iterator to meet ForwardIterator conditions, and std::advance only needs the input iterator (unless you use negative distances).

+2
Feb 22 '13 at 5:02
source share



All Articles