lazy_split_view
takes a
view
and a delimiter, and splits the
view
into subranges on the delimiter.
Two major scenarios are supported:
input_range
, the delimiter is a single element (wrapped in a single_view
).forward_range
, the delimiter is a view
of elements.The exposition-only concept
/*tiny-range*/<Pattern>is satisfied if
Pattern
satisfies
sized_range
,
Pattern::size()is a constant expression and suitable as a template constant argument, and the value of
Pattern::size()is less than or equal to
1
. Notably,
empty_view
and
single_view
satisfy this concept.
lazy_split_view
models the concepts forward_range
and input_range
when the underlying view
V
models respective concepts, and models common_range
when V
models both forward_range
and common_range
.
The inner range (ranges::range_reference_t<lazy_split_view>) models the concepts forward_range
and input_range
when the underlying view
V
models respective concepts. It does not model common_range
, and cannot be used with algorithms that expect a bidirectional_range
or higher.
Unlike split_view
, lazy_split_view
does not maintain the continuity of the subrange.
V
base_
(private) the underlying view
Pattern
pattern_
(private) the pattern that is used as a delimiter to split the underlying view
current_
(private)
V
does not satisfy forward_range
) an object that caches the result of calls to begin()
lazy_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
The name lazy_split_view
is introduced by the post-C++20 defect report P2210R2. It has the same lazy mechanism as that of the old split_view
before change.
#include <algorithm> #include <iostream> #include <ranges> #include <string_view> auto print = [](auto const& view) { // `view` is of std::views::lazy_split_view::__outer_iterator::value_type for (std::cout << "{ "; const auto element : view) std::cout << element << ' '; std::cout << "} "; }; int main() { constexpr static auto source = {0, 1, 0, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9}; constexpr int delimiter{0}; constexpr std::ranges::lazy_split_view outer_view{source, delimiter}; std::cout << "splits[" << std::ranges::distance(outer_view) << "]: "; for (auto const& inner_view: outer_view) print(inner_view); constexpr std::string_view hello{"Hello C++ 20 !"}; std::cout << "\n" "substrings: "; std::ranges::for_each(hello | std::views::lazy_split(' '), print); constexpr std::string_view text{"Hello-+-C++-+-20-+-!"}; constexpr std::string_view delim{"-+-"}; std::cout << "\n" "substrings: "; std::ranges::for_each(text | std::views::lazy_split(delim), print); }
Output:
splits[5]: { } { 1 } { 2 3 } { 4 5 6 } { 7 8 9 } substrings: { H e l l o } { C + + } { 2 0 } { ! } substrings: { H e l l o } { C + + } { 2 0 } { ! }[edit] Defect reports
The 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 moves its functionality to lazy_split_view
[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