std::ranges::split_view<V,Pattern>::split_view
From cppreference.com
< cpp | ranges | split view
split_view() requires std::default_initializable<V> && |
(1) | (since C++20) |
constexpr split_view( V base, Pattern pattern ); |
(2) | (since C++20) |
template< ranges::forward_range R > requires std::constructible_from<V, views::all_t<R>> && |
(3) | (since C++20) |
Constructs a split_view
.
Let base_
be the underlying view and pattern_
be the delimiter.
1) Default constructor. Value-initializes the
base_
and the pattern_
with their default member initializers respectively.2) Initializes the
base_
with std::move(base) and the pattern_
with std::move(pattern).3) Initializes the
base_
with views::all(std::forward<R>(r)) and the pattern_
with ranges::single_view{std::move(e)}.Parameters
base | - | the view (to be split) |
pattern | - | view to be used as the delimiter |
e | - | element to be used as the delimiter |
Example
A link to check the code: wandbox
Run this code
#include <string_view> #include <algorithm> #include <iterator> #include <iostream> #include <iomanip> #include <ranges> #include <vector> #include <array> int main() { { auto view = std::views::iota(1, 20) | std::views::transform([](int x) { return x % 5; }); auto splitts = std::views::split(view, 0); // (2) for (const auto& split : splitts) { std::cout << "{ "; std::ranges::copy(split, std::ostream_iterator<int>(std::cout, " ")); std::cout << "} "; } } std::cout << '\n'; { const std::vector nums{ 1, -1, -1, 2, 3, -1, -1, 4, 5, 6 }; const std::array delim{ -1, -1 }; auto splitter = std::views::split(nums, delim); // (3) for (const auto& split : splitter) { std::cout << "{ "; std::ranges::copy(split, std::ostream_iterator<int>(std::cout, " ")); std::cout << "} "; } } std::cout << '\n'; { constexpr std::string_view JupiterMoons{"Callisto, Europa, Ganymede, Io, and 75 more"}; constexpr std::string_view delim{", "}; std::ranges::split_view moons_extractor{ JupiterMoons, delim }; // (3) for (const std::string_view moon : moons_extractor) { std::cout << std::quoted(moon) << ' '; } } }
Output:
{ 1 2 3 4 } { 1 2 3 4 } { 1 2 3 4 } { 1 2 3 4 } { 1 } { 2 3 } { 4 5 6 } "Callisto" "Europa" "Ganymede" "Io" "and 75 more"
See also
(C++20) |
constructs a lazy_split_view (public member function of std::ranges::lazy_split_view<V,Pattern> ) |