|
Range primitives | |||||||
|
Range concepts | |||||||||||||||||||
|
Range factories | |||||||||
|
Range adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
Helper items | |||||||||||||||||
|
Member functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(C++26) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Defined in header <ranges> | ||
template<ranges::view V> requiresranges::input_range<V> | (1) | (since C++23) |
template<ranges::view V> requiresranges::forward_range<V> | (2) | (since C++23) |
namespace views{ inlineconstexpr/* unspecified */ chunk=/* unspecified */; | (3) | (since C++23) |
Call signature | ||
template<ranges::viewable_range R> constexprranges::viewauto chunk( R&& r,ranges::range_difference_t<R> n); | (since C++23) | |
template<class DifferenceType> constexpr/*range adaptor closure*/ chunk( DifferenceType&& n); | (since C++23) | |
Helper templates | ||
template<class I> constexpr I/*div-ceil*/( I num, I denom); | (4) | (exposition only*) |
chunk_view
takes aview
and a numbern and produces a range of views (thechunks ) of the original view, such that eachchunk , except maybe the last one, has the sizen
. Thesechunks are non-overlapping, successive sub-ranges of the elements of the original view, in order.
Lets
be the size of the original view. Ifs
is not the multiple ofn, the size of thelast produced view is exactlys% n (the remainder). Otherwise, the size of eachchunk , including the last one, isn.
The size of produced view is/*div-ceil*/(s).
If then is not greater than0 the behavior is undefined.
V
that modelsforward_range
or stronger. Modelscommon_range
if the underlying viewV
isforward_range
,common_range
, and eithersized_range
or nonbidirectional_range
.I r= num/ denom;if(num% denom)++r;return r;
Contents |
Member | Description |
V base_ | the underlying view (exposition-only member object*) |
ranges::range_difference_t<V>n_ | the “chunk” size (exposition-only member object*) |
If | |
ranges::range_difference_t<V>remainder_ (conditionally present) | the number of elements left in the current “chunk” (exposition-only member object*) |
non-propagating-cache<ranges::iterator_t<V>>current_ (conditionally present) | an object that caches current underlying iterator (exposition-only member object*) |
constructs achunk_view (public member function)[edit] | |
returns a copy of the underlying (adapted) view (public member function)[edit] | |
returns an iterator to the beginning (public member function)[edit] | |
returns an iterator or a sentinel to the end (public member function)[edit] | |
returns the number of elements, provided only if the underlying (adapted) range satisfiessized_range (public member function)[edit] | |
(C++26) | returns the approximate size of the resultingapproximately_sized_range (public member function)[edit] |
Inherited fromstd::ranges::view_interface | |
returns whether the derived view is empty, provided only if it satisfiessized_range orforward_range (public member function of std::ranges::view_interface<D> )[edit] | |
(C++23) | returns a constant iterator to the beginning of the range (public member function of std::ranges::view_interface<D> )[edit] |
(C++23) | returns a sentinel for the constant iterator of the range (public member function of std::ranges::view_interface<D> )[edit] |
returns whether the derived view is not empty, provided only ifranges::empty is applicable to it (public member function of std::ranges::view_interface<D> )[edit] | |
returns the first element in the derived view, provided if it satisfiesforward_range (public member function of std::ranges::view_interface<D> )[edit] | |
returns the last element in the derived view, provided only if it satisfiesbidirectional_range andcommon_range (public member function of std::ranges::view_interface<D> )[edit] | |
returns then th element in the derived view, provided only if it satisfiesrandom_access_range (public member function of std::ranges::view_interface<D> )[edit] |
(C++23) | the output (“chunk-wise”) iterator type whenV modelsinput_range (1)(exposition-only member class*) |
(C++23) | the inner (“element-wise”) iterator type whenV modelsinput_range (1)(exposition-only member class*) |
(C++23) | the iterator type whenV modelsforward_range (2)(exposition-only member class template*) |
template<class V> constexprboolranges::enable_borrowed_range<chunk_view<V>>= | (since C++23) | |
This specialization ofranges::enable_borrowed_range makeschunk_view
satisfyborrowed_range
when the underlying viewV
satisfies both, theforward_range
and theborrowed_range
.
IfV
modelsinput_range
(1),chunk_view
's iterator has a dedicated type:outer_iterator::value_type
that is itself an input view.
IfV
modelsforward_range
or stronger(2),chunk_view
defers toviews::take for itsvalue_type
.
IfV
modelsbidirectional_range
or stronger ranges(2), the need to calculate size the last chunk correctly (from the enditerator) requires the underlying range typeV
to besized_range
.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_ranges_chunk | 202202L | (C++23) | std::ranges::chunk_view |
#include <algorithm>#include <initializer_list>#include <iostream>#include <ranges> auto print_subrange=[](std::ranges::viewable_rangeauto&& r){std::cout<<'[';for(int pos{};auto elem: r)std::cout<<(pos++?" ":"")<< elem;std::cout<<"] ";}; int main(){constauto v={1,2,3,4,5,6}; for(constunsigned width: std::views::iota(1U, 2U+ v.size())){autoconst chunks= v| std::views::chunk(width);std::cout<<"chunk("<< width<<"): "; std::ranges::for_each(chunks, print_subrange);std::cout<<'\n';}}
Output:
chunk(1): [1] [2] [3] [4] [5] [6]chunk(2): [1 2] [3 4] [5 6]chunk(3): [1 2 3] [4 5 6]chunk(4): [1 2 3 4] [5 6]chunk(5): [1 2 3 4 5] [6]chunk(6): [1 2 3 4 5 6]chunk(7): [1 2 3 4 5 6]
splits theview into subranges between each pair of adjacent elements for which the given predicate returnsfalse(class template)(range adaptor object)[edit] | |
aview consisting of tuples of references to adjacent elements of the adapted view(class template)(range adaptor object)[edit] | |
aview whose Mth element is aview over the Mth through (M + N - 1)th elements of anotherview (class template)(range adaptor object)[edit] |