Rediger

Del via


split_view class (C++ Standard Library)

Splits a view into subranges based on a delimiter. The delimiter can be a single element or a view of elements. The delimiter isn't part of the resulting split_view.

A related view is the lazy_split_view class. The primary differences between split_view and lazy_split_view are:

View Can split a const range range type
split_view no Supports forward_range or higher.
lazy_split_view yes Supports input_range or higher.

Prefer split_view because it's more efficient unless you must split a range that is const.

Syntax

template<forward_range V, forward_range Pattern>
    requires view<V> && view<Pattern> &&
    indirectly_comparable<iterator_t<V>, iterator_t<Pattern>, ranges::equal_to>
class split_view : public view_interface<split_view<V, Pattern>>;

Template parameters

Pattern
The type of the view that specifies the delimiter sequence.

V
The type of the underlying view.

View characteristics

For a description of the following entries, see View class characteristics

Characteristic Description
Range adaptor views::split
Underlying range Same as underlying range
Element type range_reference_t<V>
View iterator category Satisfies forward_range
Sized No
Is const-iterable No
Common range Only if the underlying range satisfies common_range
Borrowed range No

Members

Member functions Description
Constructors Construct the view.
baseC++20 Get the underlying range.
beginC++20 Get an iterator to the first element.
endC++20 Get the sentinel at the end of the view.
Inherited from view_interface Description
emptyC++20 Test whether the view is empty.
frontC++20 Get the first element.
operator boolC++20 Test whether the view isn't empty.

Requirements

Header: <ranges> (since C++20)

Namespace: std::ranges

Compiler Option: /std:c++20 or later is required.

Constructors

Construct an instance of a split_view

1) split_view() requires default_initializable<V> && default_initializable<Pattern> = default;
2) constexpr split_view(V base, Pattern pattern);
3) template<input_range R> requires constructible_from<V, views::all_t<R>> &&
     constructible_from<Pattern, single_view<range_value_t<R>>>
     constexpr split_view(R&& rg, range_value_t<R> e);

Parameters

e
A single element that identifies where to split the view. The element isn't part of the resulting view.

base
The underlying view.

pattern
The view of elements that identifies where to split the view. The view of elements isn't part of the resulting view.

rg
The range to split.

For information about template parameter types, see Template parameters.

Return value

A split_view instance that contains one or more subranges.

Remarks

The best way to create a split_view is by using the views::split range adaptor. Range adaptors are the intended way to create view classes. The view types are only exposed in case you want to create your own custom view type.

1) Create a split_view that is default constructed. The underlying view and pattern are default constructed. base() returns a copy of V().
2) Create a split_view by splitting the view using a sequence of delimiters.
3) Create a split_view by splitting the view using a single delimiter.

Example split_view

// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> rg{ 1, 2, 3, 1, 2, 3, 4, 5, 6 };

    // pipe syntax using range adaptor
    for (const auto& subrange : rg | std::views::split(3))
    {
        // outputs
        // 1 2
        // 1 2
        // 4 5 6
        for (const auto& elem : subrange)
        {
            std::cout << elem << ' ';
        }
        std::cout << '\n';
    }
    
    int delimiters[] = {2, 3};
    for (auto splitRange : std::views::split(rg, delimiters)) // ctor syntax
    {
        for (auto& i : splitRange)
        {
            std::cout << i << " "; // 1 1 4 5 6
        }
    }
}
1 2
1 2
4 5 6
1 1 4 5 6

base

Gets a copy of the underlying view.

// Uses a copy constructor to return the underlying view
1) constexpr V base() const & requires std::copy_constructible<V>;

// Uses a move constructor to return the underlying view
2) constexpr V base() &&;

Parameters

None.

Returns

The underlying view.

begin

Get an iterator to the first element in the view.

constexpr auto begin();

Parameters

None.

Return value

An iterator pointing at the first element in the view.

Picture of a vector with the elements 10, 20, and 30. The first element contains 10 and is labeled begin(). The last element contains 30 and is labeled 'last element'. An imaginary box after the last element indicates the sentinel and is labeled end().

end

Get the sentinel at the end of the view.

constexpr auto end();

Parameters

None.

Return value

The sentinel that follows the last element in the view:

Picture of a vector with the elements 10, 20, and 30. The first element contains 10 and is labeled begin(). The last element contains 30 and is labeled 'last element'. An imaginary box after the last element indicates the sentinel and is labeled end().

See also

<ranges>
split_view range adaptor
lazy_split_view class
view classes