[range.iter.ops] (original) (raw)
24 Iterators library [iterators]
24.4 Iterator primitives [iterator.primitives]
24.4.4 Range iterator operations [range.iter.ops]
24.4.4.1 General [range.iter.ops.general]
The library includes the function templatesranges::advance, ranges::distance,ranges::next, and ranges::prevto manipulate iterators.
These operations adapt to the set of operators provided by each iterator category to provide the most efficient implementation possible for a concrete iterator type.
[Example 1:
ranges::advance uses the + operator to move arandom_access_iterator forward n steps in constant time.
For an iterator type that does not model random_access_iterator,ranges::advance instead performs n individual increments with the ++ operator.
— _end example_]
The entities defined in [range.iter.ops] are algorithm function objects ([alg.func.obj]).
24.4.4.2 ranges::advance [range.iter.op.advance]
Effects:
Effects:
- If I and S model assignable_from<I&, S>, equivalent to i = std::move(bound).
- Otherwise, if S and I model sized_sentinel_for<S, I>, equivalent to ranges::advance(i, bound - i).
- Otherwise, while bool(i != bound) is true, increments i.
Preconditions: If n > 0, [i, bound) denotes a range.
If n == 0, [i, bound) or [bound, i) denotes a range.
Effects:
- If S and I model sized_sentinel_for<S, I>:
- Otherwise,
Returns: n - M, where M is the difference between the ending and starting positions of i.
24.4.4.3 ranges::distance [range.iter.op.distance]
Preconditions: [first, last) denotes a range.
Effects: Increments first until last is reached and returns the number of increments.
template<class I, [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized%5Fsentinel%5Ffor "24.3.4.8 Concept sized_sentinel_for [iterator.concept.sizedsentinel]")<decay_t<I>> S> constexpr iter_difference_t<decay_t<I>> ranges::distance(I&& first, S last);
Effects: Equivalent to: return last - static_cast<const decay_t<I>&>(first);
template<[range](range.range#concept:range "25.4.2 Ranges [range.range]") R> constexpr range_difference_t<R> ranges::distance(R&& r);
Effects: If R models sized_range, equivalent to:return static_cast<range_difference_t<R>>(ranges::size(r));
Otherwise, equivalent to:return ranges::distance(ranges::begin(r), ranges::end(r));
24.4.4.4 ranges::next [range.iter.op.next]
Effects: Equivalent to: ++x; return x;
Effects: Equivalent to: ranges::advance(x, n); return x;
Effects: Equivalent to: ranges::advance(x, bound); return x;
Effects: Equivalent to: ranges::advance(x, n, bound); return x;
24.4.4.5 ranges::prev [range.iter.op.prev]
Effects: Equivalent to: --x; return x;
Effects: Equivalent to: ranges::advance(x, -n); return x;
Effects: Equivalent to: ranges::advance(x, -n, bound); return x;