std::ranges...::chunk_view(3)
π NAME
okay bestie, this is the std::ranges...::chunk_view(3) page from the C++ Standard Libary β manual name secured, let's go. π
std::ranges::views::chunk,std::ranges::chunk_view β std::ranges::views::chunk,std::ranges::chunk_view
β¨ SYNOPSIS
Defined in header <ranges>
template< ranges::view V >
requires ranges::input_range<V> (1)
(since C++23)
class chunk_view
: public ranges::view_interface<chunk_view<V>>
template< ranges::view V >
requires ranges::forward_range<V> (2)
(since C++23)
class chunk_view<V>
: public ranges::view_interface<chunk_view<V>>
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,
(since C++23)
ranges::range_difference_t<R> n );
template< class DifferenceType >
constexpr /*range adaptor closure*/ chunk( DifferenceType&& n
(since C++23)
);
Helper templates
template< class I > (4)
(exposition only*)
constexpr I __div_ceil( I num, I denom );
okay so chunk_view takes a view and a number n, and it serves you a range of views β the chunks β of the original view. Every chunk except maybe the last one has size n. These chunks are non-overlapping, successive sub-ranges of the elements of the original view, in order. no cap. π«Ά
Let s be the size of the original view. If s is not a 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 the produced view is __div_ceil(s).
Ifnis not greater than0, the behavior is undefined. bestie, do not do that. literally undefined behavior is not a vibe. π
- An implementation that supports the underlying view
Vthat models onlyinput_range. - A partial specialization that supports the underlying view
Vthat modelsforward_rangeor stronger. Modelscommon_rangeif the underlying viewVisforward_range,common_range, and eithersized_rangeor nonbidirectional_range. - The name
views::chunkdenotes aRangeAdaptorObject. Given subexpressionseandn, the expressionviews::chunk(e, n)is expression-equivalent tochunk_view(e, n). - Computes the smallest integer value that is not less than the quotient of dividing
numbydenom. Equivalent to:
I r = num / denom;
if (num % denom)
++r;
return r;
π«Ά MEMBER FUNCTIONS
constructorβ constructs achunk_view. (C++23) (public member function)baseβ returns a copy of the underlying (adapted) view. (C++23) (public member function)beginβ returns an iterator to the beginning. (C++23) (public member function)endβ returns an iterator or a sentinel to the end. (C++23) (public member function)sizeβ returns the number of elements. Provided only if the underlying (adapted) range satisfiessized_range. (C++23) (public member function)
𧬠Inherited from std::ranges::view_interface
emptyβ returns whether the derived view is empty. Provided if it satisfiessized_rangeorforward_range. (C++20) (public member function ofstd::ranges::view_interface<D>)cbeginβ returns a constant iterator to the beginning of the range. (C++23) (public member function ofstd::ranges::view_interface<D>)cendβ returns a sentinel for the constant iterator of the range. (C++23) (public member function ofstd::ranges::view_interface<D>)operator boolβ returns whether the derived view is not empty. Provided ifranges::emptyis applicable to it. (C++20) (public member function ofstd::ranges::view_interface<D>)frontβ returns the first element in the derived view. Provided if it satisfiesforward_range. (C++20) (public member function ofstd::ranges::view_interface<D>)backβ returns the last element in the derived view. Provided if it satisfiesbidirectional_rangeandcommon_range. (C++20) (public member function ofstd::ranges::view_interface<D>)operator[]β returns then^thelement in the derived view. Provided if it satisfiesrandom_access_range. (C++20) (public member function ofstd::ranges::view_interface<D>)
π DEDUCTION GUIDES
this page literally just drops the heading and moves on β no deduction guides listed here. iconic minimalism, i guess. π
π¦ NESTED CLASSES
outer_iteratorβ the output ("chunk-wise") iterator type whenVmodelsinput_range(1). (C++23) (exposition-only member class*)inner_iteratorβ the inner ("element-wise") iterator type whenVmodelsinput_range(1). (C++23) (exposition-only member class*)iteratorβ the iterator type whenVmodelsforward_range(2). (C++23) (exposition-only member class template*)
πΈ HELPER TEMPLATES
template< class V >
inline constexpr bool ranges::enable_borrowed_range<chunk_view<V>> =
(since C++23)
ranges::forward_range<V> && ranges::enable_borrowed_range<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. π«Ά
π NOTES
- If
Vmodelsinput_range(1),chunk_view's iterator has a dedicated type:outer_iterator::value_typethat is itself an input view. - If
Vmodelsforward_rangeor stronger(2),chunk_viewdefers toviews::takefor itsvalue_type. - If
Vmodelsbidirectional_rangeor stronger ranges(2), the need to calculate size the last chunk correctly (from the end iterator) requires the underlying range typeVto besized_range.
Feature-test macro:
__cpp_lib_ranges_chunkβ value202202L, std(C++23), featurestd::ranges::chunk_view.
π» EXAMPLE
// Run this code
#include <algorithm>
#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]
π REFERENCES
- C++23 standard (
ISO/IEC 14882:2023): 26.7.28 Chunk view [range.chunk]
π SEE ALSO
ranges::chunk_by_view/views::chunk_byβ splits the view into subranges between each pair of adjacent elements for which the given predicate returnsfalse. (C++23) (class template) (range adaptor object)ranges::adjacent_view/views::adjacentβ a view consisting of tuples of references to adjacent elements of the adapted view. (C++23) (class template) (range adaptor object)ranges::slide_view/views::slideβ a view whoseM^thelement is a view over theM^ththrough(M + N - 1)^thelements of another view. (C++23) (class template) (range adaptor object)
okay that's the whole page, bestie β source/version vibes: http://cppreference.com, dated 2024.06.10, for std::ranges...::chunk_view(3). bye π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms