std::ranges::views::chunk, std::ranges::chunk_view - cppreference.com (original) (raw)
Defined in header | ||
---|---|---|
template< ranges::view V > requires ranges::input_range<V> class chunk_view : public ranges::view_interface<chunk_view<V>> | (1) | (since C++23) |
template< ranges::view V > requires ranges::forward_range<V> class chunk_view<V> : public ranges::view_interface<chunk_view<V>> | (2) | (since C++23) |
namespace views { inline constexpr /* unspecified */ chunk = /* unspecified */; } | (3) | (since C++23) |
Call signature | ||
template< ranges::viewable_range R > constexpr ranges::view auto 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 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.
An implementation that supports the underlying view
V
that models only input_range.The name views::chunk denotes a RangeAdaptorObject. Given subexpressions e and n, the expression views::chunk(e, n) is expression-equivalent to chunk_view(e, n).
Computes the smallest integer value that is not less than the quotient of dividing num by denom. Equivalent to:
I r = num / denom; if (num % denom) ++r; return r;
Contents
- 1 Data members
- 2 Member functions
- 3 Deduction guides
- 4 Nested classes
- 5 Helper templates
- 6 Notes
- 7 Example
- 8 References
- 9 See also
[edit] Data members
Member | Description |
---|---|
V base_ | the underlying view(exposition-only member object*) |
ranges::range_difference_t<V> n_ | the “chunk” size(exposition-only member object*) |
[edit] Member functions
(constructor) | constructs a chunk_view (public member function) [edit] |
---|---|
base | returns a copy of the underlying (adapted) view (public member function) [edit] |
begin | returns an iterator to the beginning (public member function) [edit] |
end | returns an iterator or a sentinel to the end (public member function) [edit] |
size | returns the number of elements, provided only if the underlying (adapted) range satisfies sized_range (public member function) [edit] |
reserve_hint(C++26) | returns the approximate size of the resulting approximately_sized_range (public member function) [edit] |
Inherited from std::ranges::view_interface | |
empty | returns whether the derived view is empty, provided only if it satisfies sized_range or forward_range (public member function of std::ranges::view_interface) [edit] |
cbegin(C++23) | returns a constant iterator to the beginning of the range (public member function of std::ranges::view_interface) [edit] |
cend(C++23) | returns a sentinel for the constant iterator of the range (public member function of std::ranges::view_interface) [edit] |
operator bool | returns whether the derived view is not empty, provided only if ranges::empty is applicable to it (public member function of std::ranges::view_interface) [edit] |
front | returns the first element in the derived view, provided if it satisfies forward_range (public member function of std::ranges::view_interface) [edit] |
back | returns the last element in the derived view, provided only if it satisfies bidirectional_range and common_range (public member function of std::ranges::view_interface) [edit] |
operator[] | returns the nth element in the derived view, provided only if it satisfies random_access_range (public member function of std::ranges::view_interface) [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.
[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 #include #include #include auto print_subrange = 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]