Since only random access iterators provide
+
and
-
operators, the library provides two
function templates
advance
and
distance. 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);
Requires:
n
shall be negative only for bidirectional and random access iterators
. Effects:
Increments (or decrements for negative
n)
iterator reference
i
by
n. template <class InputIterator>
constexpr typename iterator_traits<InputIterator>::difference_type
distance(InputIterator first, InputIterator last);
Effects:
If
InputIterator meets the requirements of random access iterator,
returns
(last - first); otherwise, returns
the number of increments needed to get from
first
to
last. Requires:
If
InputIterator meets the requirements of random access iterator,
last shall be reachable from
first or
first shall be
reachable from
last; otherwise,
last
shall be reachable from
first. 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;