< cpp‎ | ranges
Defined in header <ranges>
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 non-negative 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

if T models input_or_output_iterator and decltype((F)) models std::convertible_to<std::iter_difference_t<T>>,
Otherwise, views::counted(E, F) is ill-formed.

[edit] Expression-equivalent

Expression e is expression-equivalent to expression f, if e and f have the same effects, either are both potentially-throwing or are both not potentially-throwing (i.e. noexcept(e) == noexcept(f)), and either are both constant subexpressions or are both not constant subexpressions.

[edit] Example

#include <ranges>
#include <iostream>
int main()
    const int a[] = {1, 2, 3, 4, 5, 6, 7};
    for(int i : std::views::counted(a, 3))
        std::cout << i << ' ';
    std::cout << '\n';
    const auto il = {1, 2, 3, 4, 5};
    for (int i : std::views::counted(il.begin() + 1, 3))
        std::cout << i << ' ';
    std::cout << '\n';


1 2 3 
2 3 4

[edit] See also

combines an iterator-sentinel pair into a view
(class template) [edit]
iterator adaptor that tracks the distance to the end of the range
(class template) [edit]
returns the number of elements satisfying specific criteria
(niebloid) [edit]