| title | view_interface class (C++ Standard Library) | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| description | API reference for the Standard Template Library (STL) <ranges> view_interface class, which is the base class for the ranges view classes. | ||||||||
| ms.date | 10/07/2022 | ||||||||
| f1_keywords |
|
||||||||
| helpviewer_keywords |
|
||||||||
| dev_langs |
|
The base class for the view classes in the std::ranges namespace. This class implements some of the interface for derived view types. Use this as the base class for your own view types to reduce the boilerplate you need to write.
template<class Derived>
requires std::is_class_v<Derived> &&
std::same_as<Derived, std::remove_cv_t<Derived>>
class view_interface;Derived
The type of the class that is deriving from this base class.
| Member functions | Description |
|---|---|
backC++20 |
Get the last element in the derived view. |
dataC++20 |
Get a pointer to the first element in the derived view. |
emptyC++20 |
Test whether the derived view is empty. |
frontC++20 |
Get the first element in the derived view. |
sizeC++20 |
Get the number of elements in the derived view. |
| Operators | Description |
operator[]C++20 |
Get the element at the specified position. |
operator boolC++20 |
Test whether the derived view isn't empty. |
Header: <ranges> (since C++ 20)
Namespace: std::ranges
Compiler Option: /std:c++20 or later is required.
Get the last element in the derived view.
constexpr auto back()
requires ranges::bidirectional_range<Derived> &&
ranges::common_range<Derived>;
constexpr auto back() const
requires ranges::bidirectional_range<const Derived> &&
ranges::common_range<const Derived>;None.
The last element in the derived view.
The derived view must satisfy bidirectional_range and common_range.
The behavior of back() and front() are undefined for any empty view.
Get a pointer to the first element in the derived view.
constexpr auto data()
requires std::contiguous_iterator<ranges::iterator_t<Derived>>;
constexpr auto data() const
requires ranges::range<const Derived> &&
std::contiguous_iterator<ranges::iterator_t<const Derived>>;None.
A pointer to the first element in the derived view.
The iterator for the derived view must satisfy contiguous_iterator.
Test whether the derived view is empty.
1) constexpr bool empty() requires ranges::forward_range<Derived>;
2) constexpr bool empty() const requires ranges::forward_range<const Derived>;None.
Returns true if the derived view has no elements. Otherwise, returns false.
The derived view must satisfy std::ranges::forward_range.
Get the first element in the derived view.
constexpr auto front()
requires ranges::forward_range<Derived>;
constexpr auto front() const
requires ranges::forward_range<const Derived>;None.
The last element in the derived view.
The derived view must satisfy forward_range.
The behavior of front() is undefined for std::ranges::empty_view.
Get the number of elements in the derived view.
constexpr auto size() requires ranges::forward_range<Derived> &&
std::sized_sentinel_for<ranges::sentinel_t<Derived>,
ranges::iterator_t<Derived>>;
constexpr auto size() const requires ranges::forward_range<const Derived> &&
std::sized_sentinel_for<ranges::sentinel_t<const Derived>,
ranges::iterator_t<const Derived>>;None.
The number of elements in the derived view.
The iterator for the derived view must satisfy sized_sentinel_for.
Get the element at the specified position.
template<ranges::random_access_range R = Derived>
constexpr decltype(auto) operator[](ranges::range_difference_t<R> pos);
template<ranges::random_access_range R = const Derived>
constexpr decltype(auto) operator[](ranges::range_difference_t<R> pos) const;pos
The position, relative to the beginning iterator, of the element to return.
The element at the specified position relative to the beginning iterator.
The derived view must satisfy random_access_range.
The behavior of this operator is undefined for std::ranges::empty_view.
// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v{1, 2, 3, 4, 5};
std::ranges::drop_view dv = std::views::drop(v, 2);
for (auto e : dv)
{
std::cout << e << ' '; // 3 4 5
}
std::cout << "\ndv[1] = " << dv[1];
}3 4 5
dv[1] = 4
Test whether the derived view isn't empty.
explicit constexpr operator bool();
explicit constexpr operator bool() const;None.
Returns false if the derived view has no elements (the view is empty). Otherwise, returns true (the view isn't empty).
The iterator for the derived view must satisfy std::ranges::forward_iterator.
This operator is equivalent to !empty(). This makes it convenient to write if (someRange) {...} to test whether there's something in the range to operate on.
The behavior of this operator is undefined for std::ranges::empty_view.
// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v{1, 2, 3, 4, 5};
std::ranges::filter_view fv = std::views::filter(v, [](int e) { return e > 3; });
bool isNotEmpty = static_cast<bool>(fv);
std::cout << "Has elements greater than 3: " << std::boolalpha << isNotEmpty << '\n' >>;
}Has elements greater than 3: true
<ranges>
ranges::begin()
ranges::data()
ranges::end()
ranges::empty()
ranges::size()
View classes