Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | ranges
m (Top: + navbar, + <ranges>)
m (Data members: fmt)
 
(47 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{cpp/ranges/view title | split}}
+
{{cpp/ranges/view title|split}}
 
{{cpp/ranges/split_view/navbar}}
 
{{cpp/ranges/split_view/navbar}}
  
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | ranges}}
+
{{dcl header|ranges}}
{{dcl | num=1 | since=c++20 |1=
+
{{dcl|num=1|since=c++20|1=
template< ranges::input_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::forward_range<V> {{!!}} /*tiny-range*/<Pattern>)
+
                                    ranges::iterator_t<Pattern>,
class split_view : public ranges::view_interface<split_view<V, Pattern>>
+
                                    ranges::equal_to>
 +
class split_view
 +
    : public ranges::view_interface<split_view<V, Pattern>>
 
}}
 
}}
{{dcl | num=2 | since=c++20 |1=
+
{{dcl|num=2|since=c++20|1=
 
namespace views {
 
namespace views {
     inline constexpr /*unspecified*/ split = /*unspecified*/;
+
     inline constexpr /* unspecified */ split = /* unspecified */;
 
}
 
}
 +
}}
 +
{{dcl h|Call signature}}
 +
{{dcl|since=c++20|1=
 +
template< ranges::viewable_range R, class Pattern >
 +
    requires /* see below */
 +
constexpr ranges::view auto split( R&& r, Pattern&& pattern );
 +
}}
 +
{{dcl|since=c++20|1=
 +
template< class Pattern >
 +
constexpr /* range adaptor closure */ split( Pattern&& pattern );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
 +
<!--add an extended wording-->
  
 
@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.
<!---->
 
Two major scenarios are supported:
 
* The view is a {{lconcept|input_range}}, the delimiter is a single element (wrapped in a {{rlpt|single_view}}).
 
* The view is a {{lconcept|forward_range}}, the delimiter is a {{lconcept|view}} of elements.
 
<!---->
 
The exposition-only concept {{c|/*tiny-range*/<Pattern>}} is satisfied if {{c|Pattern}} satisfies {{lconcept|sized_range}}, {{c|Pattern::size()}} is a constant expression, and the value of {{c|Pattern::size()}} is less than or equal to {{tt|1}}.<!-- The standard uses `std::remove_reference_t<R>::size()`, but since `Pattern` is a `view`, it cannot be a reference. --> Notably, {{rlpt|empty_view}} and {{rlpt|single_view}} satisfy this concept.
 
  
@2@ The expression {{c|views::split(E,F)}} is ''expression-equivalent'' to {{c|split_view{E,F} }}.
+
@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.
 +
 
 +
The inner range ({{c/core|ranges::range_reference_t<split_view>}}) is a {{c/core|ranges::subrange<ranges::iterator_t<V>>}}, which models {{lconcept|common_range}}, models {{lconcept|sized_range}} when {{c/core|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.
 +
 
 +
===Data members===
 +
{{dsc begin}}
 +
{{dsc hitem|Member name|Definition}}
 +
{{dsc expos mem obj|base_|private=yes|the underlying (adapted) {{lconcept|view}} of type {{tt|V}}}}
 +
{{dsc expos mem obj|pattern_|private=yes|the pattern object that is used as a delimiter to split the underlying {{lconcept|view}}}}
 +
{{dsc expos mem obj|cached_begin_|private=yes|an [[cpp/ranges#Non-propagating cache|''optional-like'']] object of type {{c/core|/*non-propagating-cache*/<ranges::subrange<ranges::iterator_t<V>>>}} that caches the result of the first call to {{rltf|begin}}}}
 +
{{dsc end}}
  
 
===Member functions===
 
===Member functions===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/ranges/adaptor/dsc constructor | split_view }}
+
{{dsc inc|cpp/ranges/adaptor/dsc constructor|split_view}}
{{dsc inc | cpp/ranges/adaptor/dsc base | split_view }}
+
{{dsc inc|cpp/ranges/adaptor/dsc base|split_view}}
{{dsc inc | cpp/ranges/adaptor/dsc begin | split_view }}
+
{{dsc inc|cpp/ranges/adaptor/dsc begin|split_view}}
{{dsc inc | cpp/ranges/adaptor/dsc end | split_view }}
+
{{dsc inc|cpp/ranges/adaptor/dsc end|split_view}}
 +
{{dsc expos mem fun|cpp/ranges/split_view/find_next|searches for the next occurrence of the pattern}}
 +
 
 +
{{cpp/ranges/view_interface/inherit|embedded=yes|data=invalid|back=invalid|size=invalid|operator[]=invalid}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Nested classes===
 
===Nested classes===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc mem class| cpp/ranges/split_view/outer-iterator | title=''outer-iterator'' | the iterator type}}
+
{{dsc expos mem class|cpp/ranges/split_view/iterator|the iterator type}}
{{dsc mem class| cpp/ranges/split_view/inner-iterator | title=''inner-iterator'' | the iterator type of the inner range}}  
+
{{dsc expos mem class|cpp/ranges/split_view/sentinel|the sentinel type}}
 
{{dsc end}}
 
{{dsc end}}
  
 
==={{rl|deduction guides|Deduction guides}}===
 
==={{rl|deduction guides|Deduction guides}}===
 +
 +
===Notes===
 +
Before {{wg21|P2210R2}}, {{tt|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 {{wg21|P2210R2}}, and the lazy mechanism is moved to {{rlpt|lazy_split_view}}.
 +
 +
The delimiter {{tt|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 {{lc|std::string_view}} literal instead.
  
 
===Example===
 
===Example===
 
{{example
 
{{example
 
|code=
 
|code=
 +
#include <iomanip>
 
#include <iostream>
 
#include <iostream>
 
#include <ranges>
 
#include <ranges>
Line 54: Line 82:
 
int main()
 
int main()
 
{
 
{
     constexpr static auto source = {0, 1,0, 2,3,0, 4,5,6,0, 7,8,9};
+
     using std::operator""sv;
    constexpr int delimiter{0};
+
     constexpr auto words{"Hello^_^C++^_^20^_^!"sv};
    constexpr std::ranges::split_view split_view{source, delimiter};
+
     constexpr auto delim{"^_^"sv};
     std::cout << "number of splits: " << std::ranges::distance(split_view)
+
              << "\n" "splits: ";
+
     for (auto const& split: split_view) {
+
        std::cout << "{ ";
+
        for (const int element : split)
+
            std::cout << element << ' ';
+
        std::cout << "} ";
+
    }
+
  
    constexpr std::string_view hello{ "Hello C++ 20 !" };
+
     for (const auto word : std::views::split(words, delim))
    std::cout << "\n" "substrings: ";
+
        // with string_view's C++23 range constructor:
     for (auto const& split: std::ranges::views::split(hello, ' ')) {
+
         std::cout << std::quoted(std::string_view(word)) << ' ';
         std::cout << "{ ";
+
    std::cout << '\n';
        for (const char element : split)
+
            std::cout << element;
+
        std::cout << " } ";
+
    }
+
 
}
 
}
| output=
+
|output=
number of splits: 5
+
"Hello" "C++" "20" "!"
splits: { } { 1 } { 2 3 } { 4 5 6 } { 7 8 9 }
+
substrings: { Hello } { C++ } { 20 } { ! }
+
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|paper=P2210R2|std=C++20|before=the old {{tt|split_view}} was too lazy to be easily used|after=it is redesigned}}
 +
{{dr list end}}
 +
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/ranges/dsc lazy_split_view}}
 +
{{dsc inc|cpp/ranges/dsc join_view}}
 +
{{dsc end}}
 +
 +
{{langlinks|de|es|ja|ru|zh}}

Latest revision as of 11:01, 13 July 2024

 
 
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

[edit] Data members

Member name Definition
base_ (private) the underlying (adapted) view of type V
(exposition-only member object*)
pattern_ (private) the pattern object that is used as a delimiter to split the underlying view
(exposition-only member object*)
cached_begin_ (private) an optional-like object of type /*non-propagating-cache*/<ranges::subrange<ranges::iterator_t<V>>> that caches the result of the first call to begin()
(exposition-only member object*)

[edit] 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]
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]

[edit] Nested classes

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

[edit] Deduction guides

[edit] 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.

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.

[edit] Example

#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:

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

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

[edit] 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]