| ||||||||||||||||||||||
| Range primitives | |||||||
| |||||||
| Range concepts | |||||||||||||||||||
| |||||||||||||||||||
| Range factories | |||||||||
| |||||||||
| Range adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Helper items | |||||||||||||||||
| |||||||||||||||||
Defined in header <ranges> | ||
inlineconstexpr/*unspecified*/ counted=/*unspecified*/; | (since C++20) | |
Call signature | ||
template<class Iterator,class DifferenceType> requires/* see below */ | (since C++20) | |
A counted view presents aview of the elements of thecounted range[i, n) for some iteratori and non-negative integern.
A counted range[i, n) is then elements starting with the element pointed to byi and up to but not including the element, if any, pointed to by the result ofn applications of++i.
Ifn==0, the counted range is valid and empty. Otherwise, the counted range is only valid ifn is positive,i is dereferenceable, and[++i, --n) is a valid counted range.
Formally, ifit andcount are expressions,T isstd::decay_t<decltype((it))>, andD isstd::iter_difference_t<T>, then
T modelsinput_or_output_iterator anddecltype((count)) modelsstd::convertible_to<D>,T modelscontiguous_iterator, thenviews::counted(it, count) isexpression-equivalent tostd::span(std::to_address(it),static_cast<std::size_t>(static_cast<D>(count))),T modelsrandom_access_iterator, thenviews::counted(it, count) isexpression-equivalent toranges::subrange(it, it+static_cast<D>(count)),Contents |
The nameviews::counted denotes acustomization point object, which is a constfunction object of aliteralsemiregular class type. SeeCustomizationPointObject for details.
views::counted does not check if the range is long enough to provide allcount elements: useviews::take if that check is necessary.
#include <iostream>#include <ranges> int main(){constint a[]{1,2,3,4,5,6,7};for(int i: std::views::counted(a,3))std::cout<< i<<' ';std::cout<<'\n'; constauto il={1,2,3,4,5};for(int i: std::views::counted(il.begin()+1,3))std::cout<< i<<' ';std::cout<<'\n';}
Output:
1 2 32 3 4
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 tostd::size_t might be invalid | made explicit |
(C++20) | aview consisting of the first N elements of anotherview(class template)(range adaptor object)[edit] |
(C++20) | combines an iterator-sentinel pair into aview(class template)[edit] |
(C++20) | iterator adaptor that tracks the distance to the end of the range (class template)[edit] |
(C++20)(C++20) | returns the number of elements satisfying specific criteria (algorithm function object)[edit] |