| Iterator concepts | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Iterator primitives | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Algorithm concepts and utilities | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Indirect callable concepts | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Common algorithm requirements | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Utilities | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Iterator adaptors | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <iterator> | ||
Call signature | ||
template<class I,std::sentinel_for<I> S> requires(!std::sized_sentinel_for<S, I>) | (1) | (since C++20) |
template<class I,std::sized_sentinel_for<std::decay_t<I>> S> constexprstd::iter_difference_t<std::decay_t<I>> | (2) | (since C++20) |
template<ranges::range R> constexprranges::range_difference_t<R> | (3) | (since C++20) |
The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:
Contents |
| first | - | iterator pointing to the first element |
| last | - | sentinel denoting the end of the rangefirst is an iterator to |
| r | - | range to calculate the distance of |
R modelsranges::sized_range, returnsranges::size(r); otherwiseranges::distance(ranges::begin(r),ranges::end(r)).R modelsranges::sized_range or ifstd::sized_sentinel_for<ranges::sentinel_t<R>,ranges::iterator_t<R>> is modeled, complexity is constant; otherwise linear.struct distance_fn{template<class I,std::sentinel_for<I> S> requires(!std::sized_sentinel_for<S, I>)constexprstd::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>constexprstd::iter_difference_t<I> operator()(const I& first, S last)const{return last- first;} template<ranges::range R>constexprranges::range_difference_t<R> operator()(R&& r)const{ifconstexpr(ranges::sized_range<std::remove_cvref_t<R>>)returnstatic_cast<ranges::range_difference_t<R>>(ranges::size(r));elsereturn(*this)(ranges::begin(r),ranges::end(r));}}; inlineconstexprauto distance= distance_fn{}; |
#include <cassert>#include <forward_list>#include <iterator>#include <vector> int main(){std::vector<int> v{3,1,4};assert(std::ranges::distance(v.begin(), v.end())==3);assert(std::ranges::distance(v.end(), v.begin())==-3);assert(std::ranges::distance(v)==3); std::forward_list<int> l{2,7,1};// auto size = std::ranges::size(l); // error: not a sizable rangeauto size= std::ranges::distance(l);// OK, but aware O(N) complexityassert(size==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 ofLWG issue 3392 maderanges::distance reject array arguments | accepts them |
(C++20) | advances an iterator by given distance or to a given bound (algorithm function object)[edit] |
(C++20)(C++20) | returns the number of elements satisfying specific criteria (algorithm function object)[edit] |
| returns the distance between two iterators (function template)[edit] |