Defined in header <iterator> | ||
---|---|---|
Call signature | ||
template< class I, std::sentinel_for<I> S > requires (!std::sized_sentinel_for<S, I>) constexpr std::iter_difference_t<I> distance( I first, S last ); | (1) | (since C++20) |
template< class I, std::sized_sentinel_for<std::decay_t<I>> S > constexpr std::iter_difference_t<std::decay_t<I>> distance( I&& first, S last ); | (2) | (since C++20) |
template< ranges::range R > constexpr ranges::range_difference_t<R> distance( R&& r ); | (3) | (since C++20) |
first
to last
.r
as a signed integer.The function-like entities described on this page are niebloids, that is:
In practice, they may be implemented as function objects, or with special compiler extensions.
first | - | iterator pointing to the first element |
last | - | sentinel denoting the end of the range first is an iterator to |
r | - | range to calculate the distance of |
first
to last
.last - static_cast<const std::decay_t<I>&>(first)
.ranges::sized_range
ranges::sized_range
struct distance_fn { template<class I, std::sentinel_for<I> S> requires (!std::sized_sentinel_for<S, I>) constexpr std::iter_difference_t<I> operator()(I first, S last) const { std::iter_difference_t<I> result = 0; while (first != last) { ++first; ++result; } return result; } template<class I, std::sized_sentinel_for<std::decay<I>> S> constexpr std::iter_difference_t<I> operator()(const I& first, S last) const { return last - first; } template<ranges::range R> constexpr ranges::range_difference_t<R> operator()(R&& r) const { if constexpr (ranges::sized_range<std::remove_cvref_t<R>>) return static_cast<ranges::range_difference_t<R>>(ranges::size(r)); else return (*this)(ranges::begin(r), ranges::end(r)); } }; inline constexpr auto distance = distance_fn{}; |
#include <iostream> #include <iterator> #include <vector> int main() { std::vector<int> v{3, 1, 4}; std::cout << "distance(first, last) = " << std::ranges::distance(v.begin(), v.end()) << '\n' << "distance(last, first) = " << std::ranges::distance(v.end(), v.begin()) << '\n' << "distance(v) = " << std::ranges::distance(v) << '\n'; }
Output:
distance(first, last) = 3 distance(last, first) = -3 distance(v) = 3
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3392 | C++20 | overload (1) takes iterator by value, thus move-only iterator lvalue with a sized sentinel was rejected | added overload (2) |
LWG 3664 | C++20 | the resolution of LWG issue 3392 maderanges::distance reject array arguments | accepts them |
(C++20) | advances an iterator by given distance or to a given bound (niebloid) |
(C++20)(C++20) | returns the number of elements satisfying specific criteria (niebloid) |
returns the distance between two iterators (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/iterator/ranges/distance