title | description | ms.date | f1_keywords | helpviewer_keywords | dev_langs | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
values_view class (C++ Standard Library) |
API reference for the Standard Template Library (STL) <ranges> values_view class, which provides a view over the second index into each tuple-like value in a collection. It's useful for extracting values from associative containers. |
10/07/2022 |
|
|
|
A view consisting of the second index into each tuple-like value in a collection. For example, given a range of std::tuple<string, int>
values, create a view consisting of all the int
elements from each tuple.
values_view
is an alias for elements_view<R, 1>
and is useful for creating a view of the values in associative containers such as std::unordered_map
.
template<input_range R>
using values_view = ranges::elements_view<R, 1>;
R
The type of the underlying range. This type must satisfy ranges::input_range
.
For a description of the following entries, see View class characteristics
Characteristic | Description |
---|---|
Range adaptor | views::values |
Underlying range | Must satisfy forward_range or higher |
Element type | Same as the type of the second tuple element |
View iterator category | forward_range , bidirectional_range , or random_access_range |
Sized | Only if the underlying range satisfies sized_range |
Is const -iterable |
Only if the underlying range satisfies const-iterable |
Common range | Only if the underlying range satisfies common_range |
Borrowed range | Only if the underlying range satisfies borrowed_range |
Member functions | Description |
---|---|
ConstructorsC++20 | Construct a values_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 values_view . |
size C++20 |
Get the number of elements. |
Inherited from view_interface |
Description |
back C++20 |
Get the last element. |
empty C++20 |
Test whether the values_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 values_view isn't empty. |
Header: <ranges>
(since C++20)
Namespace: std::ranges
Compiler Option: /std:c++20
or later is required.
Tuple-like types that you can use with values_view
are std::tuple
, std::pair
, and std::array
.
Construct an instance of a values_view
.
1) constexpr values_view(R base);
2) values_view() requires default_initializable<V> = default;
base
The underlying range of tuple-like types.
For information about the template parameter type, see Template parameters.
An values_view
instance.
The best way to create an values_view
is by using the values
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.
- Create an
values_view
from the specified view. - The default constructor creates a default-constructed
values_view
.
// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
int main()
{
// ========== work with a std::map
std::map<std::string, int> cpp_standards
{
{"C++98", 1988},
{"C++03", 2003},
{"C++11", 2011},
{"C++14", 2014},
{"C++17", 2017},
{"C++20", 2020}
};
// Extract all of the values from the map
for (int years : std::views::values(cpp_standards))
{
std::cout << years << ' '; // 2003 2011 2014 2017 1988 2020
}
std::cout << '\n';
// ========== work with a std::pair
std::vector<std::pair<std::string, int>> windows
{
{"Windows 1.0", 1985},
{"Windows 2.0", 1987},
{"Windows 3.0", 1990},
{"Windows 3.1", 1992},
{"Windows NT 3.1", 1993},
{"Windows 95", 1995},
{"Windows NT 4.0", 1996},
{"Windows 95", 1995},
{"Windows 98", 1998},
{"Windows 1.0", 1985},
{"Windows 2000", 2000}
};
// Another way to call the range adaptor using '|': create a keys_view from each pair
for (int years : windows | std::views::values)
{
std::cout << years << ' '; // 1985 1987 1990 1992 ...
}
}
2003 2011 2014 2017 1988 2020
1985 1987 1990 1992 1993 1995 1996 1995 1998 1985 2000
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() &&;
None
The underlying view.
Get an iterator to the first element in the values_view
.
// returns a non-const iterator
1) constexpr auto begin() requires (!Simple_view<V>);
// returns a const iterator
2) constexpr auto begin() const requires ranges::range<const V>;
None.
An iterator pointing at the first element in the values_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 values_view
1) constexpr auto end() requires (!Simple_view<V> && !ranges::common_range<V>);
2) constexpr auto end() requires (!Simple_view<V> && ranges::common_range<V>);
3) constexpr auto end() const requires ranges::range<const V>;
4) constexpr auto end() const requires ranges::common_range<const V>;
None.
The sentinel that follows the last element in the values_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() requires sized_range<V>
constexpr auto size() const requires sized_range<const V>
None.
The number of elements in the values_view
.
The size of the view is only available if the underlying range is a sized_range
, or in other words, bounded.