From cppreference.com
Defined in header
template<classR>usingvalues_view=ranges::elements_view<R,1>; (1) (since C++20)namespaceviews{inlineconstexprautovalues=ranges::elements<1>;} (2) (since C++20)Takes a
of tuple-like values (e.g.
or
), 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>.
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(autoconst&value:std::views::values(map))std::cout<<value<<' ';// prints: alpha betaExample
Run this code
#include<iostream>#include<map>#include<ranges>intmain(){constautolist={std::pair{1,11.1},{2,22.2},{3,33.3}};std::cout<<"pair::second values in the list: ";for(doublevalue:list|std::views::values)std::cout<<value<<' ';std::map<char,int>map{{'A',1},{'B',2},{'C',3},{'D',4},{'E',5}};autoodd=[](intx){return0!=(x&1);};std::cout<<"\nodd values in the map: ";for(intvalue: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
C++20 keys_view is unable to participate in CTAD due to its use of views::all_tviews::all_t removed See also