From cppreference.com
Defined in header
template<ranges::viewV>requiresranges::input_range<V>classchunk_view:publicranges::view_interface<chunk_view<V>> (1)(since C++23)template<ranges::viewV>requiresranges::forward_range<V>classchunk_view<V>:publicranges::view_interface<chunk_view<V>> (2)(since C++23)namespaceviews{inlineconstexpr/* unspecified */chunk=/* unspecified */;} (3) (since C++23)Call signature
template<ranges::viewable_rangeR>constexprranges::viewautochunk(R&&r,ranges::range_difference_t<R>n);(since C++23)template<classDifferenceType>constexpr/*range adaptor closure*/chunk(DifferenceType&&n);(since C++23)Helper templates
template<classI>constexprI/*div-ceil*/(Inum,Idenom); (4)(exposition only*)chunk_view takes a
and a number n and produces a range of views (the chunks ) of the original view, such that each chunk , except maybe the last one, has the size n. These chunks are non-overlapping, successive sub-ranges of the elements of the original view, in order.
Let s be the size of the original view. If s is not the multiple of n, the size of the last produced view is exactly s%n (the remainder). Otherwise, the size of each chunk , including the last one, is n.
The size of produced view is /*div-ceil*/(s).
If the n is not greater than 0 the behavior is undefined.
1) An implementation that supports the underlying view V that models only
.
3) The name views::chunk denotes a
. Given subexpressions e and n, the expression views::chunk(e,n) is
to chunk_view(e,n).
4) Computes the smallest integer value that is not less than the quotient of dividing num by denom. Equivalent to:
Ir=num/denom;if(num%denom)++r;returnr;Data members
Member Description Vbase_the underlying view
(exposition-only member object*)ranges::range_difference_t<V>n_the “chunk” size
(exposition-only member object*) If V models exactly
(
)ranges::range_difference_t<V>remainder_
(conditionally present)the number of elements left in the current “chunk”
(exposition-only member object*)
<ranges::iterator_t<V>>current_
(conditionally present)an object that caches current underlying iterator
(exposition-only member object*)Member functions
constructs a chunk_view
(public member function)
returns a copy of the underlying (adapted) view
(public member function)
returns an iterator to the beginning
(public member function)
returns an iterator or a sentinel to the end
(public member function)
returns the number of elements, provided only if the underlying (adapted) range satisfies
(public member function)
(C++26)
returns the approximate size of the resulting
(public member function)
Inherited from
returns whether the derived view is empty, provided only if it satisfies
or
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a constant iterator to the beginning of the range
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a sentinel for the constant iterator of the range
(public member function of std::ranges::view_interface<D>)
returns whether the derived view is not empty, provided only if
is applicable to it
(public member function of std::ranges::view_interface<D>)
returns the first element in the derived view, provided if it satisfies
(public member function of std::ranges::view_interface<D>)
returns the last element in the derived view, provided only if it satisfies
and
(public member function of std::ranges::view_interface<D>)
returns the nth element in the derived view, provided only if it satisfies
(public member function of std::ranges::view_interface<D>)
Nested classes
Helper templates
template<classV>constexprboolranges::enable_borrowed_range<chunk_view<V>>=ranges::forward_range<V>&&ranges::enable_borrowed_range<V>;(since C++23)This specialization of
makes chunk_view satisfy
when the underlying view V satisfies both, the
and the
.
Notes
If V models
(
), chunk_view's iterator has a dedicated type:
that is itself an input view.
If V models
or stronger (
), chunk_view defers to views::take for its value_type.
If V models
or stronger ranges (
), the need to calculate size the last chunk correctly (from the end
) requires the underlying range type V to be
.
macroValueStdFeature
(C++23)std::ranges::chunk_viewExample
Run this code
#include<algorithm>#include<initializer_list>#include<iostream>#include<ranges>autoprint_subrange=[](std::ranges::viewable_rangeauto&&r){std::cout<<'[';for(intpos{};autoelem:r)std::cout<<(pos++?" ":"")<<elem;std::cout<<"] ";};intmain(){constautov={1,2,3,4,5,6};for(constunsignedwidth:std::views::iota(1U,2U+v.size())){autoconstchunks=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] References
C++23 standard (ISO/IEC 14882:2024):
26.7.28 Chunk view [range.chunk]
See also