std::ranges::views::chunk, std::ranges::chunk_view
input_range
s <ranges>
requires ranges::input_range <V>
class chunk_view
requires ranges::forward_range <V>
class chunk_view<V>
inline constexpr /* unspecified */ chunk = /* unspecified */;
constexpr ranges::view auto chunk( R&& r, ranges::range_difference_t <R> n );
constexpr /*range adaptor closure*/ chunk( DifferenceType&& n );
constexpr I /*div-ceil*/( I num, I denom );
chunk_view
takes a view
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.
V
that models forward_range
or stronger. Models common_range
if the underlying view V
is forward_range
, common_range
, and either sized_range
or non bidirectional_range
.I r = num / denom; if (num % denom) ++r; return r;
Contents
[edit] Data members
V
base_
the underlying view(exposition-only member object*)
If V
models exactly input_range
(1)
remainder_
(conditionally present) the number of elements left in the current "chunk"
(exposition-only member object*)
current_
(conditionally present) an object that caches current underlying iterator
(exposition-only member object*)
[edit] Member functions
sized_range
(public member function) [edit]
approximately_sized_range
(public member function) [edit]
Inherited from std::ranges::view_interface
sized_range
or forward_range
(public member function of
std::ranges::view_interface<D>
) [edit]
(public member function of
std::ranges::view_interface<D>
) [edit]
(public member function of
std::ranges::view_interface<D>
) [edit]
(public member function of
std::ranges::view_interface<D>
) [edit]
forward_range
(public member function of
std::ranges::view_interface<D>
) [edit]
bidirectional_range
and common_range
(public member function of
std::ranges::view_interface<D>
) [edit]
n
th element in the derived view, provided only if it satisfies random_access_range
(public member function of
std::ranges::view_interface<D>
) [edit]
[edit] Deduction guides
[edit] Nested classes
V
models input_range
(1)(exposition-only member class*)
V
models input_range
(1)(exposition-only member class*)
[edit] Helper templates
constexpr bool ranges::enable_borrowed_range <chunk_view<V>> =
This specialization of ranges::enable_borrowed_range makes chunk_view
satisfy borrowed_range
when the underlying view V
satisfies both, the forward_range
and the borrowed_range
.
[edit] Notes
If V
models input_range
(1), chunk_view
's iterator has a dedicated type: outer_iterator::value_type
that is itself an input view.
If V
models forward_range
or stronger (2), chunk_view
defers to views::take for its value_type
.
If V
models bidirectional_range
or stronger ranges (2), the need to calculate size the last chunk correctly (from the end iterator) requires the underlying range type V
to be sized_range
.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_ranges_chunk |
202202L |
(C++23) | std::ranges::chunk_view
|
[edit] Example
#include <algorithm> #include <initializer_list> #include <iostream> #include <ranges> auto print_subrange = [](std::ranges::viewable_range auto&& r) { std::cout << '['; for (int pos{}; auto elem : r) std::cout << (pos++ ? " " : "") << elem; std::cout << "] "; }; int main() { const auto v = {1, 2, 3, 4, 5, 6}; for (const unsigned width : std::views::iota (1U, 2U + v.size())) { auto const 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]
[edit] References
- C++23 standard (ISO/IEC 14882:2024):
- 26.7.28 Chunk view [range.chunk]
[edit] See also
view
into subranges between each pair of adjacent elements for which the given predicate returns false(class template) (range adaptor object)[edit]
view
consisting of tuples of references to adjacent elements of the adapted view(class template) (range adaptor object)[edit]