Files
2025-10-25 03:02:53 +03:00

2.7 KiB

[iterator.operations]

24 Iterators library [iterators]

24.4 Iterator primitives [iterator.primitives]

24.4.3 Iterator operations [iterator.operations]

1

#

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

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);

2

#

Preconditions: n is negative only for bidirectional iterators.

3

#

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);

4

#

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

5

#

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);

6

#

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

🔗

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

7

#

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