[iterator.operations] (original) (raw)

24 Iterators library [iterators]

24.4 Iterator primitives [iterator.primitives]

24.4.3 Iterator operations [iterator.operations]

Since only random access iterators provide+and-operators, the library provides two function templatesadvanceanddistance.

These function templates use+and-for random access iterators (and are, therefore, constant time for them); for input, forward and bidirectional iterators they use++to provide linear time implementations.

template<class InputIterator, class Distance> constexpr void advance(InputIterator& i, Distance n);

Preconditions: nis negative only for bidirectional iterators.

Effects: Increments i by n if n is non-negative, and decrements i by -n otherwise.

template<class InputIterator> constexpr typename iterator_traits<InputIterator>::difference_type distance(InputIterator first, InputIterator last);

Preconditions: last is reachable from first, orInputIterator meets the Cpp17RandomAccessIterator requirements andfirst is reachable from last.

Effects: If InputIterator meets the Cpp17RandomAccessIterator requirements, returns (last - first); otherwise, incrementsfirst until last is reached and returns the number of increments.

template<class InputIterator> constexpr InputIterator next(InputIterator x,typename iterator_traits<InputIterator>::difference_type n = 1);

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

template<class BidirectionalIterator> constexpr BidirectionalIterator prev(BidirectionalIterator x,typename iterator_traits<BidirectionalIterator>::difference_type n = 1);

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