Scroll to navigation

std::ranges::views::values,std::ranges::values_view(3) C++ Standard Libary std::ranges::views::values,std::ranges::values_view(3)

NAME

std::ranges::views::values,std::ranges::values_view - std::ranges::views::values,std::ranges::values_view

Synopsis


Defined in header <ranges>
template< class R > (1) (since C++20)
using values_view = ranges::elements_view<R, 1>;
namespace views {


inline constexpr auto values = ranges::elements<1>; (2) (since C++20)


}


Takes a view of tuple-like values (e.g. std::tuple or std::pair), and produces a
view with a value-type of the second element of the adapted view's value-type.


1) An alias for ranges::elements_view<R, 1>.
2) RangeAdaptorObject (and also RangeAdaptorClosureObject). The expression
views::values(e) is expression-equivalent to
values_view<views::all_t<decltype((e))>>{e} for any suitable subexpression e.

Notes


values_view can be useful for extracting values from associative containers, e.g.


std::map<int, std::string> map{{1, "alpha"}, {2, "beta"}};
for (auto const& value : std::views::values(map))
std::cout << value << ' ';
// prints: alpha beta

Example

// Run this code


#include <iostream>
#include <map>
#include <ranges>


int main()
{
const auto list = {std::pair{1, 11.1}, {2, 22.2}, {3, 33.3}};
std::cout << "pair::second values in the list: ";
for (double value : list | std::views::values)
std::cout << value << ' ';


std::map<char, int> map{{'A', 1}, {'B', 2}, {'C', 3}, {'D', 4}, {'E', 5}};
auto odd = [](int x) { return 0 != (x & 1); };
std::cout << "\nodd values in the map: ";
for (int value : map | std::views::values | std::views::filter(odd))
std::cout << value << ' ';
std::cout << '\n';
}

Output:


pair::second values in the list: 11.1 22.2 33.3
odd values in the map: 1 3 5


Defect reports


The following behavior-changing defect reports were applied retroactively to
previously published C++ standards.


DR Applied to Behavior as published Correct behavior
LWG 3563 C++20 keys_view is unable to participate in CTAD views::all_t removed
due to its use of views::all_t

See also


ranges::keys_view takes a view consisting of pair-like values and produces a
views::keys view of the first elements of each pair
(C++20) (class template) (range adaptor object)
ranges::elements_view takes a view consisting of tuple-like values and a number N
views::elements and produces a view of N^th element of each tuple
(C++20) (class template) (range adaptor object)
slice BLAS-like slice of a valarray: starting index, length, stride
(class)

2024.06.10 http://cppreference.com