title | description | ms.date | f1_keywords | helpviewer_keywords | dev_langs | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
common_view class (C++ Standard Library) |
API reference for the Standard Template Library (STL) <ranges> common_view class, which takes a view that has different iterator and sentinel types and creates a view that has the same iterator and sentinel type. |
09/27/2022 |
|
|
|
Take a range that may have different iterator and sentinel types and create a view that has the same iterator and sentinel type. This is useful for calling STL algorithms that accept ranges specified by iterator pairs.
template<ranges::view V>
requires (!ranges::common_range<V> && std::copyable<ranges::iterator_t<V>>)
class common_view : public ranges::view_interface<common_view<V>>;
V
The type of the underlying view.
For a description of the following entries, see View class characteristics
Characteristic | Description |
---|---|
Range adaptor | views::common |
Underlying range | Must satisfy forward_range or higher |
Element type | Same as the underlying range |
View iterator category | forward_range or random_access_range when the underlying range satisfies random_access_range and sized_range |
Sized | Only if the underlying range satisfies sized_range |
Is const -iterable |
Only if the underlying range is const iterable |
Common range | Yes |
Borrowed range | Only if the underlying range satisfies borrowed_range |
Member functions | Description |
---|---|
ConstructorsC++20 | Construct a common_view . |
base C++20 |
Get the underlying view. |
begin C++20 |
Get an iterator to the first element in the view. |
end C++20 |
Get the sentinel at the end of the view. |
size C++20 |
Get the number of elements in the view. |
Inherited from view_interface |
Description |
back C++20 |
Get the last element. |
data C++20 |
Get a pointer to the first element. |
empty C++20 |
Test whether the view is empty. |
front C++20 |
Get the first element. |
operator[] C++20 |
Get the element at the specified position. |
operator bool C++20 |
Test whether the view isn't empty. |
The best way to create a common_view
is by using the views::common
range adaptor. Range adaptors are the intended way to create view classes. The view types are exposed in case you want to create your own custom view type.
This view is useful for passing a range that has different iterator/sentinel types to a legacy algorithm that expects them to be the same.
Header: <ranges>
(since C++20)
Namespace: std::ranges
Compiler Option: /std:c++20
or later is required.
Create an instance of a common_view
.
1) common_view() = default;
2) constexpr explicit common_view(V v);
v
The underlying view.
For information about the template parameter type, see Template parameters.
1) Default constructs the common_view
.
2) Constructs a common_view
from the underlying view using std::move(v)
. An error will result if V
is a common range to avoid misuse that would negatively impact performance.
Gets a copy of the underlying view.
// Uses a copy constructor to return the underlying view
constexpr V base() const& requires std::copy_constructible<V>;
// Uses a move constructor to return the underlying view
constexpr V base() &&;
Get an iterator to the first element.
constexpr auto begin();
constexpr auto begin() const requires range<const V>;
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();
constexpr auto end() const requires ranges::range<const V>;
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().":::
Get the number of elements in the view.
constexpr auto size() requires ranges::sized_range<V>;
constexpr auto size() const requires ranges::sized_range<const V>;
None.
The number of elements in the view.