Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | ranges
m (Data members: + third member)
m (Member functions: + view_interface: inherited member functions)
Line 41: Line 41:
 
{{dsc inc | cpp/ranges/adaptor/dsc end | split_view }}
 
{{dsc inc | cpp/ranges/adaptor/dsc end | split_view }}
 
{{dsc priv mem fun | cpp/ranges/split_view/find_next | finds the next subrange; the name is for exposition only }}
 
{{dsc priv mem fun | cpp/ranges/split_view/find_next | finds the next subrange; the name is for exposition only }}
 +
 +
{{cpp/ranges/view_interface/inherit|embedded=yes|data=invalid|back=invalid|size=invalid}}
 
{{dsc end}}
 
{{dsc end}}
  

Revision as of 09:09, 24 July 2021

 
 
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)


1) split_view takes a view and a delimiter, and splits the view into subranges on the delimiter.
2) Ranges adaptor object. 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 contiguous_range, random_access_range, bidirectional_range, forward_range, and common_range when the underlying view V models respective concepts.

Contents

Expression-equivalent

Expression e is expression-equivalent to expression f, if

  • e and f have the same effects, and
  • either both are constant subexpressions or else neither is a constant subexpression, and
  • either both are potentially-throwing or else neither is potentially-throwing (i.e. noexcept(e) == noexcept(f)).

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]
finds the next subrange; the name is for exposition only
(private 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]
returns the nth element in the derived view. Provided if it satisfies random_access_range.
(public member function of std::ranges::view_interface<D>) [edit]

Nested classes

the iterator type
(public member class)
the sentinel type
(public member class)

Deduction guides

Notes

Example

A link to check the example: wandbox

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

Output:

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

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]