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

Header: <ranges>

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.

# Declarations

template< class R >
using values_view = ranges::elements_view<R, 1>;

(since C++20)

namespace views {
inline constexpr auto values = ranges::elements<1>;
}

(since C++20)

# Notes

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

# Example

#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';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 3563C++20keys_view is unable to participate in CTAD due to its use of views::all_tviews::all_t removed

# See also