title | description | ms.date | f1_keywords | helpviewer_keywords | dev_langs | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ref_view class (C++ Standard Library) |
API reference for the Standard Template Library (STL) <ranges> ref_view class, which is a view that references the elements of another range. It's essentially a view of the elements of another range. |
10/05/2022 |
|
|
|
A view that references the elements that belong to another range.
template<std::ranges::range R>
requires std::is_object_v<R>
class ref_view : public ranges::view_interface<ref_view<R>>;
R
The range to reference.
Member functions | Description |
---|---|
ConstructorsC++20 | Construct a ref_view . |
base C++20 |
Get a reference to the underlying range. |
begin C++20 |
Get an iterator to the first element. |
data C++20 |
Get a pointer to the first element in the referenced range. |
empty C++20 |
Test whether this ref_view is empty. |
end C++20 |
Get the sentinel at the end of this ref_view . |
size C++20 |
Get the number of elements. The underlying range must satisfy sized_range . |
Inherited from view_interface |
Description |
back C++20 |
Get the last element. |
front C++20 |
Get the first element. |
operator[] C++20 |
Get the element at the specified position. |
operator bool C++20 |
Test whether this ref_view isn't empty. |
For a description of the following entries, see View class characteristics
Characteristic | Description |
---|---|
Range adaptor | views::all or views::common |
Underlying range | Must satisfy input_range |
Element type | Same as the underlying range |
View iterator category | Same as the underlying range |
Sized | Only if the underlying range satisfies sized_range |
Is const -iterable |
Yes |
Common range | Only if the underlying range satisfies common_range |
Borrowed range | Yes |
Header: <ranges>
(since C++20)
Namespace: std::ranges
Compiler Option: /std:c++20
or later is required.
Construct an instance of a ref_view
// construct a ref_view from a range
template<different-from<ref_view> R>
constexpr ref_view(R&& rg);
rg
The range to reference.
For information about the template parameter type, see Template parameters.
A ref_view
instance.
The best way to create a ref_view
is by using the views::all
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.
A ref_view
is useful for converting a container to a view. For example, you can use ref_view
to convert a vector
to a view, which makes it inexpensive to pass the elements of the vector around.
// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v = {1,2,3};
auto refView = std::views::all(v);
std::cout << &refView[1] << " : " << &v[1]; // outputs two identical memory addresses, e.g. 00000239AFAFDF90 : 00000239AFAFDF90
refView[0] = 10; // modifies v[0]
std::cout << "\n" << v[0]; // 10
}
00000239AFAFDF90 : 00000239AFAFDF90
10
Gets a copy of the underlying range.
constexpr R& base() const;
None.
The underlying range.
Get an iterator to the first element in the ref_view
.
constexpr iterator_t<R> begin() const;
None.
An iterator pointing at the first element in this ref_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 a pointer to the first element in this ref_view
. The elements in the range must be contiguous.
constexpr auto data() const requires contiguous_range<R>;
None.
A pointer to the first element.
Test whether this ref_view
is empty.
constexpr bool empty() const
None.
Returns true
if the ref_view
contains no elements. Otherwise, false
.
Get the sentinel at the end of this ref_view
.
constexpr sentinel_t<R> end() const
The sentinel that follows the last element in this ref_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.
constexpr auto size() const requires sized_range<R>
None.
The number of elements in the ref_view
.