Rediger

Del via


ref_view class (C++ Standard Library)

A view that references the elements that belong to another range.

Syntax

template<std::ranges::range R>
    requires std::is_object_v<R>
class ref_view : public ranges::view_interface<ref_view<R>>;

Template parameters

R
The range to reference.

Members

Member functions Description
ConstructorsC++20 Construct a ref_view.
baseC++20 Get a reference to the underlying range.
beginC++20 Get an iterator to the first element.
dataC++20 Get a pointer to the first element in the referenced range.
emptyC++20 Test whether this ref_view is empty.
endC++20 Get the sentinel at the end of this ref_view.
sizeC++20 Get the number of elements. The underlying range must satisfy sized_range.
Inherited from view_interface Description
backC++20 Get the last element.
frontC++20 Get the first element.
operator[]C++20 Get the element at the specified position.
operator boolC++20 Test whether this ref_view isn't empty.

View characteristics

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

Requirements

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

Namespace: std::ranges

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

Constructors

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);

Parameters

rg
The range to reference.

For information about the template parameter type, see Template parameters.

Return value

A ref_view instance.

Remarks

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.

Example: ref_view

// 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

base

Gets a copy of the underlying range.

constexpr R& base() const;

Parameters

None.

Return value

The underlying range.

begin

Get an iterator to the first element in the ref_view.

constexpr iterator_t<R> begin() const;

Parameters

None.

Return value

An iterator pointing at the first element in this ref_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().

data

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>;

Parameters

None.

Return value

A pointer to the first element.

empty

Test whether this ref_view is empty.

constexpr bool empty() const

Parameters

None.

Return value

Returns true if the ref_view contains no elements. Otherwise, false.

end

Get the sentinel at the end of this ref_view.

constexpr sentinel_t<R> end() const

Return value

The sentinel that follows the last element in this ref_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().

size

Get the number of elements.

constexpr auto size() const requires sized_range<R>

Parameters

None.

Return value

The number of elements in the ref_view.

See also

<ranges>
all range adaptor
view classes