title | description | ms.date | f1_keywords | helpviewer_keywords | dev_langs | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
split_view class (C++ Standard Library) |
API reference for the Standard Template Library (STL) <ranges> split_view class. Splits a view into subranges based on a delimiter. The delimiter can be a single element or a view of elements. |
10/05/2022 |
|
|
|
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
.
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>>;
Pattern
The type of the view that specifies the delimiter sequence.
V
The type of the underlying view.
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 |
Member functions | Description |
---|---|
Constructors | Construct the view. |
base C++20 |
Get the underlying range. |
begin C++20 |
Get an iterator to the first element. |
end C++20 |
Get the sentinel at the end of the view. |
Inherited from view_interface |
Description |
empty C++20 |
Test whether the view is empty. |
front C++20 |
Get the first element. |
operator bool C++20 |
Test whether the view isn't empty. |
Header: <ranges>
(since C++20)
Namespace: std::ranges
Compiler Option: /std:c++20
or later is required.
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);
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.
A split_view
instance that contains one or more subranges.
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.
// 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
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() &&;
None.
The underlying view.
Get an iterator to the first element in the view.
constexpr auto begin();
None.
An iterator pointing at the first element in the view.
:::image type="content" source="media/begin-end-sentinel.png" alt-text="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().":::
Get the sentinel at the end of the view.
constexpr auto end();
None.
The sentinel that follows the last element in the view:
:::image type="content" source="media/begin-end-sentinel.png" alt-text="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().":::
<ranges>
split_view
range adaptor
lazy_split_view
class
view classes