std::ranges...::split_view(3)
π std::ranges...::split_view(3)
okay bestie, welcome to the C++ Standard Libary glow-up of the manual page for std::ranges::split_view::split_view, section (3). This is the constructor page, and we are keeping every single requires clause because we respect her. π«Άβ¨
πΈ NAME πΈ
std::ranges::split_view::split_view β std::ranges::split_view::split_view (yes, that's the whole name, she's iconic like that π
)
β¨ Synopsis β¨
Defined in header <ranges>
split_view()
requires std::default_initializable<V> &&
std::default_initializable<Pattern> = default;
(1) (since C++20)
constexpr explicit split_view( V base, Pattern pattern );
(2) (since C++20)
template< ranges::forward_range R >
requires std::constructible_from<V, views::all_t<R>> &&
std::constructible_from<Pattern, ranges::single_view<
ranges::range_value_t<R>>>
constexpr explicit split_view( R&& r, ranges::range_value_t<R> e );
(3) (since C++20)
Constructs a split_view. π
Let base_ be the underlying view and pattern_ be the delimiter.
- Default constructor. Value-initializes
base_andpattern_with their default member initializers respectively. - Initializes
base_withstd::move(base)andpattern_withstd::move(pattern). - Initializes
base_withviews::all(std::forward<R>(r))andpattern_withranges::single_view{std::move(e)}.
π Parameters π
baseβ the view (to be split)patternβ view to be used as the delimitereβ element to be used as the delimiter
π Example π
here's the whole example, no skips, no cap:
// Run this code
#include <algorithm>
#include <array>
#include <cctype>
#include <iostream>
#include <iterator>
#include <ranges>
#include <string_view>
#include <vector>
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 91 more"
};
constexpr std::string_view delim{", "};
std::ranges::split_view moons_extractor{JupiterMoons, delim}; // (3)
auto is_moon = std::views::filter([](auto str)
{
return std::isupper(str[0]);
});
std::cout << "Some moons of Jupiter: ";
for (const auto moon : moons_extractor | is_moon)
std::cout << std::string_view(moon) << ' ';
}
std::cout << '\n';
}
Output:
{ 1 2 3 4 } { 1 2 3 4 } { 1 2 3 4 } { 1 2 3 4 }
{ 1 } { 2 3 } { 4 5 6 }
Some moons of Jupiter: Callisto Europa Ganymede Io
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR
LWG 3714/(P2711R1)β applied toC++20; behavior as published: the multi-parameter constructor was not explicit; correct behavior: made explicit.
π See also π
constructorβ constructs alazy_split_view(C++20) (public member function ofstd::ranges::lazy_split_view<V,Pattern>)
source/version/date unlock: http://cppreference.com Β· 2024.06.10 Β· std::ranges...::split_view(3) β mwah, bye bestie πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms