views::transform
C++23Apply a function to each element lazily.
Section hub
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.
template< ranges::view V >
requires ranges::input_range<V>
class chunk_view
: public ranges::view_interface<chunk_view<V>>
(since C++23)
template< ranges::view V >
requires ranges::forward_range<V>
class chunk_view<V>
: public ranges::view_interface<chunk_view<V>>
(since C++23)
namespace views {
inline constexpr /* unspecified */ chunk = /* unspecified */;
}
(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 );
(exposition only*)
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 <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';
}
}
This hub groups the ranges library by user task rather than by raw reference tree shape. View types and adaptor objects are presented as the same conceptual item.
Start here for the adapters most people reach for first when building pipelines.
Apply a function to each element lazily.
Keep only elements that satisfy a predicate.
Keep the first N elements from a source range.
Skip the first N elements and expose the rest.
Split a range into non-overlapping fixed-size subranges.
Flatten a range of ranges into a single lazy sequence.
These adapt shape, ownership, or projection rather than representing the “headline” pipeline steps.
Normalize a range into a view-compatible form.
Adapt iterator/sentinel pairs into a common-range shape.
Wrap an existing range by reference.
Store and expose a range with unique ownership.
Package iterator + sentinel as a view-like object.
Project tuple-like elements to their key component.
Project tuple-like elements to their value component.
Newer adapters, kept as a compact scan list with only standard badges.
A lighter-weight index of the full ranges surface, grouped by conceptual task instead of raw page-tree names.