Namespaces
Variants
Views
Actions

std::ranges::views::split, std::ranges::split_view

From cppreference.com
< cpp‎ | ranges
Revision as of 02:49, 18 July 2021 by Space Mission (Talk | contribs)

 
 
Ranges library
Range adaptors
 
 
Defined in header <ranges>
template< ranges::input_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> &&
  (ranges::forward_range<V> || /*tiny-range*/<Pattern>)

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.

Two major scenarios are supported:

The exposition-only concept /*tiny-range*/<Pattern> is satisfied if Pattern satisfies sized_range, Pattern::size() is a constant expression, and the value of Pattern::size() is less than or equal to 1. Notably, empty_view and single_view satisfy this concept.
2) The expression views::split(E,F) is expression-equivalent to split_view{E,F}.

Contents

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]

Nested classes

the iterator type
(public member class)
the iterator type of the inner range
(public member class)

Deduction guides

Example

#include <algorithm>
#include <iostream>
#include <ranges>
#include <string_view>
 
auto print = [](auto const& view)
{
    // `view` is a std::views::split_view::/*outer-iterator*/::value_type
 
    for (std::cout << "{ "; const auto element : view)
        std::cout << element << ' ';
    std::cout << "} ";
};
 
int main()
{
    constexpr static auto source = { 0, 1,0, 2,3,0, 4,5,6,0, 7,8,9 };
    constexpr int delimiter {0};
    constexpr std::ranges::split_view split_view {source, delimiter};
    std::cout << "splits[" << std::ranges::distance(split_view) << "]:  ";
    for (auto const& split: split_view)
        print(split);
 
    constexpr std::string_view hello { "Hello C++ 20 !" };
    std::cout << "\n" "substrings: ";
    std::ranges::for_each(hello | std::views::split(' '), print);
}

Output:

splits[5]:  { } { 1 } { 2 3 } { 4 5 6 } { 7 8 9 } 
substrings: { H e l l o } { C + + } { 2 0 } { ! }

See also

a view consisting of the sequence obtained from flattening a view of ranges
(class template) (range adaptor object)[edit]