# How to Split Ranges in C++23

DevFeed: [How to Split Ranges in C++23](<https://devfeed.tech/articles/how-to-split-ranges-in-c-23-53314.md>)

Original publisher: [Read original article](<https://www.cppstories.com/2025/ranges_split_chunk/>)

Published: 2025-05-16T00:00:00Z

Content type: tutorial

Language: en

Sources: [C++ Stories](<https://devfeed.tech/sources/c-stories.md>)

Topics: [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [c-plus-plus-23](<https://devfeed.tech/tags/c-plus-plus-23.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [how-to](<https://devfeed.tech/tags/how-to.md>)

## AI overview

A practical tutorial on splitting and grouping ranges in C++ using the C++20 views::split and views::lazy_split adaptors, plus the C++23 views::chunk and views::chunk_by adaptors. It explains range requirements and demonstrates text, generic-range, stream, fixed-size batch, packet, parity, and sentence-processing examples.

## Source excerpt

In this blog post, we'll continue looking at ranges and this time explore ways to split them into sub-ranges. So we'll take a look at views::split, views::chunk, and views::chunk_by. We'll walk through two examples for each adaptor: one simple and one slightly more advanced, to highlight their practical uses. Let's go. Splitting Ranges with views::split, C++20 If you want to split a range using some "delimeter," then views::split (or ranges::split_view) will do the job. template< ranges::forward_range V, ranges::forward_range Pattern > // .. requires... class split_vie : public ranges::view_interface<split_view<V, Pattern>> Both of the ranges V and Pattern have to be forward_range. Notice that you need to use a pattern; it cannot be a condition. Example 1: Splitting a Sentence into Words #include <print> #include <ranges> #include <string_view> int main() { using namespace std::string_view_literals; constexpr auto text = "C++ is powerful and elegant"sv; for (auto part : std::views::split(text, ' ')) std::print("'{}' ", std::string_view(part)); } Play @Compiler Explorer The output: 'C++' 'is' 'powerful' 'and' 'elegant' A classic example that splits a sentence into words. We convert each subrange to std::string_view for easy printing. We can also extend this and use not just one character: constexpr auto text = "C++breakisbreakpowerfulbreakandbreakelegant"sv; for (auto part : std::views::split(text, "break"sv)) std::print("'{}' ", std::string_view(part)); Example 2: Splitting Not Just Text split can be applied to any forward range, so not just text: #include <print> #include <ranges> #include <vector> #include <utility> int main() { using Point = std::pair<int, int>; std::vector<Point> path = { {0, 0}, {1, 1}, {-1, -1}, // marker: {-1, -1} {2, 2}, {3, 3}, {-1, -1}, {4, 4}, {5, 5} }; for (auto segment : std::views::split(path, Point{-1, -1})) std::print("Segment: {}\n", segment); } See @Compiler Explorer And the output: Segment: [(0, 0), (1, 1)] Segment: [(2, 2), (3, 3