Section hub

std::ranges::views::chunk, std::ranges::chunk_view

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.

# Declarations

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*)

# 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.

# Example

#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';
    }
}

# See also

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.

Core adapters

Start here for the adapters most people reach for first when building pipelines.

Utility views

These adapt shape, ownership, or projection rather than representing the “headline” pipeline steps.

New in C++23 / C++26

Newer adapters, kept as a compact scan list with only standard badges.

All entities by category

A lighter-weight index of the full ranges surface, grouped by conceptual task instead of raw page-tree names.

74 entities