Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/ranges/split view"

From cppreference.com
< cpp‎ | ranges
m (Example: fmt)
m (~expr-eq)
Line 6: Line 6:
 
{{dcl|num=1|since=c++20|1=
 
{{dcl|num=1|since=c++20|1=
 
template< ranges::forward_range V, ranges::forward_range Pattern >
 
template< ranges::forward_range V, ranges::forward_range Pattern >
requires ranges::view<V> && ranges::view<Pattern> &&
+
requires ranges::view<V> &&
  std::indirectly_comparable<ranges::iterator_t<V>, ranges::iterator_t<Pattern>,
+
        ranges::view<Pattern> &&
                            ranges::equal_to>
+
        std::indirectly_comparable<ranges::iterator_t<V>,
 +
                                    ranges::iterator_t<Pattern>,
 +
                                    ranges::equal_to>
 
class split_view :
 
class split_view :
 
     public ranges::view_interface<split_view<V, Pattern>>
 
     public ranges::view_interface<split_view<V, Pattern>>
Line 33: Line 35:
 
@1@ {{tt|split_view}} takes a {{lconcept|view}} and a delimiter, and splits the {{lconcept|view}} into subranges on the delimiter.
 
@1@ {{tt|split_view}} takes a {{lconcept|view}} and a delimiter, and splits the {{lconcept|view}} into subranges on the delimiter.
  
@2@ {{named req|RangeAdaptorObject}}. The expression {{c|views::split(e, p)}} is ''expression-equivalent'' to {{c|split_view(e, p)}}<!-- LWG 3524 --> for any suitable subexpressions {{c|e}} and {{c|p}}.
+
@2@ {{named req|RangeAdaptorObject}}. The expression {{c|views::split(e, p)}} is [[cpp/language/expressions#Expression-equivalence|expression-equivalent]] to {{c|split_view(e, p)}}<!-- LWG 3524 --> for any suitable subexpressions {{c|e}} and {{c|p}}.
  
 
{{tt|split_view}} models the concepts {{lconcept|forward_range}}, and {{lconcept|common_range}} when the underlying {{lconcept|view}} {{tt|V}} models respective concepts.
 
{{tt|split_view}} models the concepts {{lconcept|forward_range}}, and {{lconcept|common_range}} when the underlying {{lconcept|view}} {{tt|V}} models respective concepts.
  
 
The inner range ({{c|ranges::range_reference_t<split_view>}}) is a {{c|ranges::subrange<ranges::iterator_t<V>>}}, which models {{lconcept|common_range}}, models {{lconcept|sized_range}} when {{c|ranges::iterator_t<V>}} models {{c|std::sized_sentinel_for<ranges::iterator_t<V>>}}, and models {{lconcept|contiguous_range}}, {{lconcept|random_access_range}}, {{lconcept|bidirectional_range}}, and {{lconcept|forward_range}} when {{tt|V}} models respective concepts.
 
The inner range ({{c|ranges::range_reference_t<split_view>}}) is a {{c|ranges::subrange<ranges::iterator_t<V>>}}, which models {{lconcept|common_range}}, models {{lconcept|sized_range}} when {{c|ranges::iterator_t<V>}} models {{c|std::sized_sentinel_for<ranges::iterator_t<V>>}}, and models {{lconcept|contiguous_range}}, {{lconcept|random_access_range}}, {{lconcept|bidirectional_range}}, and {{lconcept|forward_range}} when {{tt|V}} models respective concepts.
 
{{cpp/expr-eq}}
 
  
 
===Data members===
 
===Data members===
 
Typical implementations of {{tt|split_view}} hold three non-static data members:
 
Typical implementations of {{tt|split_view}} hold three non-static data members:
* the underlying {{lconcept|view}} of type {{tt|V}} (shown here as {{tt|''base_''}} for exposition only), and
+
* the underlying {{lconcept|view}} of type {{tt|V}} (shown here as {{tti|base_}} for exposition only), and
* the pattern (shown here as {{tt|''pattern_''}} for exposition only) that is used as a delimiter to split the underlying {{lconcept|view}}.
+
* the pattern (shown here as {{tti|pattern_}} for exposition only) that is used as a delimiter to split the underlying {{lconcept|view}}.
* an object equivalent to {{c|1=std::optional<ranges::subrange<ranges::iterator_t<V>>>}} (shown here as {{tt|''cached_begin_''}} for exposition only) that caches the result of a first call to {{rlt|begin|begin()}}.
+
* an object equivalent to {{c|1=std::optional<ranges::subrange<ranges::iterator_t<V>>>}} (shown here as {{tti|cached_begin_}} for exposition only) that caches the result of a first call to {{rlt|begin|begin()}}.
  
 
===Member functions===
 
===Member functions===

Revision as of 13:53, 26 April 2023

 
 
Ranges library
Range adaptors
 
 
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)


1) split_view takes a view and a delimiter, and splits the view into subranges on the delimiter.
2) RangeAdaptorObject. The expression views::split(e, p) is expression-equivalent to split_view(e, p) for any suitable subexpressions e and p.

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.

Contents

Data members

Typical implementations of split_view hold three non-static data members:

  • the underlying view of type V (shown here as base_ for exposition only), and
  • the pattern (shown here as pattern_ for exposition only) that is used as a delimiter to split the underlying view.
  • an object equivalent to std::optional<ranges::subrange<ranges::iterator_t<V>>> (shown here as cached_begin_ for exposition only) that caches the result of a first call to begin().

Member functions

constructs a 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]
(C++20)
searches for the next occurrence of the pattern
(exposition-only member function*)
Inherited from std::ranges::view_interface
returns whether the derived view is empty. Provided if it satisfies sized_range or forward_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 if ranges::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 satisfies forward_range.
(public member function of std::ranges::view_interface<D>) [edit]

Nested classes

(C++20)
the iterator type
(exposition-only member class*)
(C++20)
the sentinel type
(exposition-only member class*)

Deduction guides

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.

Example

#include <iomanip>
#include <iostream>
#include <ranges>
#include <string_view>
 
int main()
{
    constexpr std::string_view words{"Hello^_^C++^_^20^_^!"};
    constexpr std::string_view delim{"^_^"};
 
    for (const auto word : std::views::split(words, delim))
        std::cout << std::quoted(std::string_view{word.begin(), word.end()}) << ' ';
}

Output:

"Hello" "C++" "20" "!"

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 old split_view was too lazy to be easily used it is redesigned

See also

a view over the subranges obtained from splitting another view using a delimiter
(class template) (range adaptor object)[edit]
a view consisting of the sequence obtained from flattening a view of ranges
(class template) (range adaptor object)[edit]