| ||||||||||||||||||||||
| Range primitives | |||||||
| |||||||
| Range concepts | |||||||||||||||||||
| |||||||||||||||||||
| Range factories | |||||||||
| |||||||||
| Range adaptors | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Helper items | |||||||||||||||||
| |||||||||||||||||
Defined in header <ranges> | ||
template<ranges::input_range V,ranges::forward_range Pattern> requiresranges::view<V>&& | (1) | (since C++20) |
namespace views{ inlineconstexpr/* unspecified */ lazy_split=/* unspecified */; | (2) | (since C++20) |
Call signature | ||
template<ranges::viewable_range R,class Pattern> requires/* see below */ | (since C++20) | |
template<class Pattern> constexpr/* range adaptor closure */ lazy_split( Pattern&& pattern); | (since C++20) | |
Helper concepts | ||
template<class R> concept/*tiny-range*/= | (3) | (exposition only*) |
lazy_split_view takes aview and a delimiter, and splits theview into subranges on the delimiter.Two major scenarios are supported:
input_range, the delimiter is a single element (wrapped in asingle_view).forward_range, the delimiter is aview of elements.Pattern satisfiessized_range,Pattern::size() is a constant expression and suitable as a template constant argument, and the value ofPattern::size() is less than or equal to1. Notably,empty_view andsingle_view satisfy this concept.lazy_split_view models the conceptsforward_range andinput_range when the underlyingviewV models respective concepts, and modelscommon_range whenV models bothforward_range andcommon_range.
The inner range (ranges::range_reference_t<lazy_split_view>) models the conceptsforward_range andinput_range when the underlyingviewV models respective concepts. It does not modelcommon_range, and cannot be used with algorithms that expect abidirectional_range or higher.
Unlikesplit_view,lazy_split_view does not maintain the continuity of the subrange.
Contents |
| Member | Description |
Vbase_(private) | the underlyingview(exposition-only member object*) |
Patternpattern_(private) | the pattern that is used as a delimiter to split the underlyingview(exposition-only member object*) |
non-propagating-cache<ranges::iterator_t<V>>current_(private)(present only if V does not satisfyforward_range) | an object that caches the result of calls tobegin()(exposition-only member object*) |
constructs alazy_split_view(public member function)[edit] | |
| returns a copy of the underlying (adapted) view (public member function)[edit] | |
| returns an iterator to the beginning (public member function)[edit] | |
| returns an iterator or a sentinel to the end (public member function)[edit] | |
Inherited fromstd::ranges::view_interface | |
returns whether the derived view is empty, provided only if it satisfiessized_range orforward_range(public member function of std::ranges::view_interface<D>)[edit] | |
(C++23) | returns a constant iterator to the beginning of the range (public member function of std::ranges::view_interface<D>)[edit] |
(C++23) | returns a sentinel for the constant iterator of the range (public member function of std::ranges::view_interface<D>)[edit] |
| returns whether the derived view is not empty, provided only ifranges::empty is applicable to it (public member function of std::ranges::view_interface<D>)[edit] | |
returns the first element in the derived view, provided if it satisfiesforward_range(public member function of std::ranges::view_interface<D>)[edit] | |
| the iterator type (exposition-only member class template*) | |
| the iterator type of the inner range (exposition-only member class template*) |
The namelazy_split_view is introduced by the post-C++20 defect reportP2210R2. It has the same lazy mechanism as that of the oldsplit_view before change.
#include <algorithm>#include <iostream>#include <ranges>#include <string_view> auto print=[](autoconst& view){// `view` is of std::views::lazy_split_view::__outer_iterator::value_type for(std::cout<<"{ ";constauto element: view)std::cout<< element<<' ';std::cout<<"} ";}; int main(){constexprstaticauto source={0,1,0,2,3,0,4,5,6,0,7,8,9};constexprint delimiter{0};constexpr std::ranges::lazy_split_view outer_view{source, delimiter};std::cout<<"splits["<< std::ranges::distance(outer_view)<<"]: ";for(autoconst& inner_view: outer_view) print(inner_view); constexprstd::string_view hello{"Hello C++ 20 !"};std::cout<<"\n""substrings: "; std::ranges::for_each(hello| std::views::lazy_split(' '), print); constexprstd::string_view text{"Hello-+-C++-+-20-+-!"};constexprstd::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 } { ! }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 tolazy_split_view |
aview over the subranges obtained from splitting anotherview using a delimiter(class template)(range adaptor object)[edit] | |
(C++20) | aview consisting of the sequence obtained from flattening aview ofranges(class template)(range adaptor object)[edit] |