std::ranges::views::counted

Header: <ranges>

A counted view presents a view of the elements of the counted range [i,n) for some iterator i and non-negative integer n.

# Declarations

inline constexpr /*unspecified*/ counted = /*unspecified*/;

(since C++20)

Call signature
template< class Iterator, class DifferenceType >
requires /* see below */
constexpr /*span-or-subrange*/ counted( Iterator&& it, DifferenceType&& count );

(since C++20)

# Notes

views::counted does not check if the range is long enough to provide all count elements: use views::take if that check is necessary.

# Example

#include <iostream>
#include <ranges>
 
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';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
P2393R1C++20implicit conversion from an integer-class type to std::size_t might be invalidmade explicit

# See also