CKTIS / C++
std::views::split — split a range into subranges
std::views::split creates a view of parts separated by an element or pattern. Parts refer to the underlying data rather than becoming separate strings; adjacent and trailing delimiters can produce empty parts.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <ranges>
std::views::split(range, delimiter)
range | std::views::split(delimiter)std::views::split from <ranges> is an adaptor that creates a std::ranges::split_view. For a character range and a delimiter such as ',', the direct call std::views::split(range, delimiter) and the pipeline form range | std::views::split(delimiter) split the range the same way. The two forms are not interchangeable when the delimiter is an array. The delimiter is one element or a range representing a complete pattern, not a set of alternative separators.
The underlying range and pattern must meet forward_range requirements, with comparable elements. Searching happens during iteration without building a collection of strings. Each part is a std::ranges::subrange containing iterators into the underlying data; the delimiter is excluded.
Adjacent delimiters produce empty parts, as does a trailing delimiter. Empty input produces no parts. In a direct call, the literal "::" serves as an array pattern that includes its terminating null character. In the pipeline form, partial application decays the array to a const char*, which is neither a pattern range nor a character delimiter. To split on :: without the null character, use std::string_view{"::"} in either form.
For borrowed data, preserve its lifetime and avoid invalidating iterators while using the view or its parts. The example uses a literal with static storage duration and prints empty parts as []. The C++23 setting is a compilation target, not evidence of production runtime support.
Examples
#include <iostream>
#include <ranges>
#include <string_view>
int main() {
constexpr std::string_view text = "red,,blue,";
for (auto part : text | std::views::split(',')) {
std::cout << '[';
for (char c : part) {
std::cout << c;
}
std::cout << "]\n";
}
}
Result
[red] [] [blue] []
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.