namespace view { inline constexpr /*unspecified*/ counted = /*unspecified*/; }  (since C++20) 
A counted view presents a View
of the elements of the counted range [i, n)
for some iterator i
and nonnegative integer n
.
A counted range [i, n)
is the n
elements starting with the element pointed to by i
and up to but not including the element, if any, pointed to by the result of n
applications of ++i
.
If n == 0
, the counted range is valid and empty. Otherwise, the counted range is only valid if n is positive, i is dereferenceable, and [++i, n)
is a valid counted range.
Formally, if E
and F
are expressions, and T
is the type std::decay_t<decltype((E))>
, then.
T
models Iterator
and decltype((F))
models ConvertibleTo<iter_difference_t<T>>
, RandomAccessIterator
, view::counted(E, F)
is expressionequivalent to ranges::subrange{E, E + static_cast<std::iter_difference_t<T>>(F)}
view::counted(E, F)
is expressionequivalent to ranges::subrange{std::counted_iterator{E, F}, std::default_sentinel}
view::counted(E, F)
is illformed. #include <ranges> #include <iostream> int main() { int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; for(int i : std::view::counted(a, 3)) std::cout << i << ' '; }
Output:
1 2 3
combines an iteratorsentinel pair into a View (class template) 

(C++20)  iterator adaptor that tracks the distance to the end of the range (class template) 
© cppreference.com
Licensed under the Creative Commons AttributionShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/ranges/view_counted