std::ranges...::split_view(3)
π std::ranges...::split_view(3) π
okay so you're on the C++ Standard Libary manual page for std::ranges...::split_view(3) β yes, βLibaryβ is literally how the header spells it, and honestly? iconic typo energy π. This page is your group chat for splitting a view on a delimiter, bestie. Periodt. π
NAME
std::ranges::views::split,std::ranges::split_view β the split besties: the range adaptor and the view that does the actual splitting. No cap, they're a package deal. π«Ά
Synopsis β¨
Defined in header <ranges>
template< ranges::forward_range V, ranges::forward_range Pattern >
requires ranges::view<V> &&
ranges::view<Pattern> &&
std::indirectly_comparable<ranges::iterator_t<V>,
ranges::iterator_t<Pattern>,
ranges::equal_to>
class split_view
: public ranges::view_interface<split_view<V, Pattern>>
(1) (since C++20)
namespace views {
inline constexpr /* unspecified */ split = /* unspecified */;
}
(2) (since C++20)
Call signature π
template< ranges::viewable_range R, class Pattern >
requires /* see below */
constexpr ranges::view auto split( R&& r, Pattern&& pattern );
(since C++20)
template< class Pattern >
constexpr /* range adaptor closure */ split( Pattern&& pattern );
(since C++20)
split_viewtakes a view and a delimiter, and splits the view into subranges on the delimiter. slay.RangeAdaptorObject. The expressionviews::split(e, p)is expression-equivalent tosplit_view(e, p)for any suitable subexpressionseandp.
split_view models the concepts forward_range, and common_range when the underlying view V models respective concepts. π¦
The inner range (ranges::range_reference_t<split_view>) is a ranges::subrange<ranges::iterator_t<V>>, which models common_range, models sized_range when ranges::iterator_t<V> models std::sized_sentinel_for<ranges::iterator_t<V>>, and models contiguous_range, random_access_range, bidirectional_range, and forward_range when V models respective concepts. literally the whole concept glow-up, iykyk. β¨
Member functions π
Here's the whole roster, no skips:
constructorβ constructs asplit_view.(C++20)(public member function)baseβ returns a copy of the underlying (adapted) view.(C++20)(public member function)beginβ returns an iterator to the beginning.(C++20)(public member function)endβ returns an iterator or a sentinel to the end.(C++20)(public member function)find_nextβ searches for the next occurrence of the pattern.(C++20)(exposition-only 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>)
Nested classes πΈ
iteratorβ the iterator type.(C++20)(exposition-only member class*)sentinelβ the sentinel type.(C++20)(exposition-only member class*)
Deduction guides π§
literally nothing listed here in the source, so I'm not inventing deductions, bestie π«Ά
Notes π
Before P2210R2, split_view used a lazy mechanism for splitting, and thus could not keep the bidirectional, random access, or contiguous properties of the underlying view, or make the iterator type of the inner range same as that of the underlying view. Consequently, it is redesigned by P2210R2, and the lazy mechanism is moved to lazy_split_view. the way that whole redesign happened? main character energy fr fr. π
The delimiter pattern generally should not be an ordinary string literal, as it will consider the null terminator to be necessary part of the delimiter; therefore, it is advisable to use a std::string_view literal instead. That's a real warning, not a vibe β do not let the null terminator crash the party π.
Example β¨
// Run this code
#include <iomanip>
#include <iostream>
#include <ranges>
#include <string_view>
int main()
{
using std::operator""sv;
constexpr auto words{"Hello^_^C++^_^20^_^!"sv};
constexpr auto delim{"^_^"sv};
for (const auto word : std::views::split(words, delim))
// with string_view's C++23 range constructor:
std::cout << std::quoted(std::string_view(word)) << ' ';
std::cout << '\n';
}
Output π
"Hello" "C++" "20" "!"
Defect reports π₯
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR:
P2210R2β Applied to:C++20β Behavior as published: the oldsplit_viewwas too lazy to be easily used β Correct behavior: it is redesigned.
See also π
ranges::lazy_split_view/views::lazy_splitβ a view over the subranges obtained from splitting another view using a delimiter.(C++20)(class template) (range adaptor object)ranges::join_view/views::joinβ a view consisting of the sequence obtained from flattening a view of ranges.(C++20)(class template) (range adaptor object)
okay bestie, that's the whole page β source: http://cppreference.com, version/date: 2024.06.10, page: std::ranges...::split_view(3). mwah πβ¨
π¦ 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