A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../algorithm/../algorithm/../ranges/counted_view.html below:

std::ranges::views::counted - cppreference.com

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)

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

A counted range [in) 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 it and count are expressions, T is std::decay_t<decltype((it))>, and D is std::iter_difference_t<T>, then

if T models input_or_output_iterator and decltype((count)) models std::convertible_to<D>,
Otherwise, views::counted(it, count) is ill-formed.
Customization point objects

The name views::counted denotes a customization point object, which is a const function object of a literal semiregular class type. See CustomizationPointObject for details.

[edit] 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.

[edit] 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';
}

Output:

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior P2393R1 C++20 implicit conversion from an integer-class type to std::size_t might be invalid made explicit [edit] See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4