From cppreference.com
Defined in header
template<ranges::input_rangeV,ranges::forward_rangePattern>requiresranges::view<V>&&ranges::view<Pattern>&&std::indirectly_comparable<ranges::iterator_t<V>,ranges::iterator_t<Pattern>,ranges::equal_to>&&(ranges::forward_range<V>||/*tiny-range*/<Pattern>)classlazy_split_view:publicranges::view_interface<lazy_split_view<V,Pattern>> (1) (since C++20)namespaceviews{inlineconstexpr/* unspecified */lazy_split=/* unspecified */;} (2) (since C++20)Call signature
template<ranges::viewable_rangeR,classPattern>requires/* see below */constexprranges::viewautolazy_split(R&&r,Pattern&&pattern);(since C++20)template<classPattern>constexpr/* range adaptor closure */lazy_split(Pattern&&pattern);(since C++20)Helper concepts
template<classR>concept/*tiny-range*/=ranges::sized_range<R>&&requires{/* is-statically-constexpr-sized */<R>;}&&(std::remove_reference_t<R>::size()<=1); (3) (exposition only*)1)lazy_split_view takes a
and a delimiter, and splits the
into subranges on the delimiter.
Two major scenarios are supported:
The view is an
, the delimiter is a single element (wrapped in a
).
The view is a
, the delimiter is a
of elements.
3) The exposition-only concept /*tiny-range*/<Pattern> is satisfied if Pattern satisfies
, Pattern::size() is a constant expression and suitable as a template non-type argument, and the value of Pattern::size() is less than or equal to 1. Notably,
and
satisfy this concept.
lazy_split_view models the concepts
and
when the underlying
V models respective concepts, and models
when V models both
and
.
The inner range (ranges::range_reference_t<lazy_split_view>) models the concepts
and
when the underlying
V models respective concepts. It does not model
, and cannot be used with algorithms that expect a
or higher.
Unlike
, lazy_split_view does not maintain the continuity of the subrange.
Data members
Member Description Vbase_(private)the underlying
(exposition-only member object*)Patternpattern_(private)the pattern that is used as a delimiter to split the underlying
(exposition-only member object*)
<ranges::iterator_t<V>>current_(private)
(present only if V does not satisfy
)an object that caches the result of calls to
(exposition-only member object*)Member functions
constructs a lazy_split_view
(public member function)
returns a copy of the underlying (adapted) view
(public member function)
returns an iterator to the beginning
(public member function)
returns an iterator or a sentinel to the end
(public member function)
Inherited from
returns whether the derived view is empty, provided only if it satisfies
or
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a constant iterator to the beginning of the range
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a sentinel for the constant iterator of the range
(public member function of std::ranges::view_interface<D>)
returns whether the derived view is not empty, provided only if
is applicable to it
(public member function of std::ranges::view_interface<D>)
returns the first element in the derived view, provided if it satisfies
(public member function of std::ranges::view_interface<D>)
Nested classes
the iterator type
(exposition-only member class template*)the iterator type of the inner range
(exposition-only member class template*)
Notes
The name lazy_split_view is introduced by the post-C++20 defect report
. It has the same lazy mechanism as that of the old split_view before change.
Example
Run this code
#include<algorithm>#include<iostream>#include<ranges>#include<string_view>autoprint=[](autoconst&view){// `view` is of std::views::lazy_split_view::__outer_iterator::value_typefor(std::cout<<"{ ";constautoelement:view)std::cout<<element<<' ';std::cout<<"} ";};intmain(){constexprstaticautosource={0,1,0,2,3,0,4,5,6,0,7,8,9};constexprintdelimiter{0};constexprstd::ranges::lazy_split_viewouter_view{source,delimiter};std::cout<<"splits["<<std::ranges::distance(outer_view)<<"]: ";for(autoconst&inner_view:outer_view)print(inner_view);constexprstd::string_viewhello{"Hello C++ 20 !"};std::cout<<"\n""substrings: ";std::ranges::for_each(hello|std::views::lazy_split(' '),print);constexprstd::string_viewtext{"Hello-+-C++-+-20-+-!"};constexprstd::string_viewdelim{"-+-"};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 } { ! } Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++20 the old split_view was too lazy to be easily used moves its functionality to lazy_split_viewSee also