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.
1)An implementation that supports the underlying view
V
that models only
input_range
.
4)Computes the smallest integer value that is not less than the quotient of dividing
numby
denom. Equivalent to:
I r = num / denom; if (num % denom) ++r; return r;[edit] Data members Member Description
V
base_
the underlying view
n_
the âchunkâ size
chunk_view
sized_range
approximately_sized_range
sized_range
or forward_range
std::ranges::view_interface<D>
) [edit] returns a constant iterator to the beginning of the range
std::ranges::view_interface<D>
) [edit] returns a sentinel for the constant iterator of the range
std::ranges::view_interface<D>
) [edit] returns whether the derived view is not empty, provided only if ranges::empty is applicable to it
std::ranges::view_interface<D>
) [edit] returns the first element in the derived view, provided if it satisfies forward_range
std::ranges::view_interface<D>
) [edit] returns the last element in the derived view, provided only if it satisfies bidirectional_range
and common_range
std::ranges::view_interface<D>
) [edit] returns the n
th element in the derived view, provided only if it satisfies random_access_range
std::ranges::view_interface<D>
) [edit] [edit] Deduction guides [edit] Nested classes [edit] Helper templates
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
.
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
.
#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
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