split_view
takes a
view
and a delimiter, and splits the
view
into subranges on the delimiter.
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.
Unlike lazy_split_view
, split_view
maintains the continuity of the subrange, making it suitable for string splitting.
V
base_
(private) the underlying (adapted) view
Pattern
pattern_
(private) the pattern object that is used as a delimiter to split the underlying view
<ranges::iterator_t<V>>> cached_begin_
(private) an object that caches the result of the first call to begin()
split_view
sized_range
or forward_range
std::ranges::view_interface<D>
) [edit] returns a constant iterator to the beginning of the range
std::ranges::view_interface<D>
) [edit] returns a sentinel for the constant iterator of the range
std::ranges::view_interface<D>
) [edit] returns whether the derived view is not empty, provided only if ranges::empty is applicable to it
std::ranges::view_interface<D>
) [edit] returns the first element in the derived view, provided if it satisfies forward_range
std::ranges::view_interface<D>
) [edit] [edit] Nested classes the iterator type
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 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.
#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:
[edit] Defect reportsThe following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior P2210R2 C++20 the oldsplit_view
was too lazy to be easily used it is redesigned [edit] See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4