Difference between revisions of "cpp/ranges/values view"
From cppreference.com
m |
Andreas Krug (Talk | contribs) m (fmt, headers sorted, +newline) |
||
(12 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/ranges/view title | values}} | + | {{cpp/ranges/view title|values}} |
{{cpp/ranges/navbar}} | {{cpp/ranges/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header | ranges}} | + | {{dcl header|ranges}} |
− | {{dcl | num=1 | since=c++20 | 1= | + | {{dcl|num=1|since=c++20|1= |
− | template<class R> | + | template< class R > |
− | using values_view = ranges::elements_view | + | using values_view = ranges::elements_view<R, 1>; |
}} | }} | ||
− | {{dcl | num=2 | since=c++20 | 1= | + | {{dcl|num=2|since=c++20|1= |
namespace views { | namespace views { | ||
inline constexpr auto values = ranges::elements<1>; | inline constexpr auto values = ranges::elements<1>; | ||
Line 17: | Line 17: | ||
Takes a {{lconcept|view}} of ''tuple-like'' values (e.g. {{lc|std::tuple}} or {{lc|std::pair}}), and produces a view with a ''value-type'' of the ''second'' element of the adapted view's value-type. | Takes a {{lconcept|view}} of ''tuple-like'' values (e.g. {{lc|std::tuple}} or {{lc|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 {{c|ranges::elements_view | + | @1@ An alias for {{c|ranges::elements_view<R, 1>}}. |
− | @2@ | + | @2@ {{named req|RangeAdaptorObject}} (and also {{named req|RangeAdaptorClosureObject}}). The expression {{c|views::values(e)}} is [[cpp/language/expressions#Expression-equivalence|expression-equivalent]] to {{c|values_view<views::all_t<decltype((e))>>{e} }} for any suitable subexpression {{c|e}}. |
− | + | ===Notes=== | |
− | + | {{lc|values_view}} can be useful for extracting ''values'' from associative containers, e.g. | |
− | === Notes === | + | {{source|1= |
− | {{lc|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 === | + | ===Example=== |
− | {{example|code= | + | {{example |
+ | |code= | ||
#include <iostream> | #include <iostream> | ||
− | |||
#include <map> | #include <map> | ||
+ | #include <ranges> | ||
int main() | int main() | ||
{ | { | ||
− | std:: | + | 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); }; | auto odd = [](int x) { return 0 != (x & 1); }; | ||
− | + | std::cout << "\nodd values in the map: "; | |
− | std::cout << " | + | |
for (int value : map {{!}} std::views::values {{!}} std::views::filter(odd)) | for (int value : map {{!}} std::views::values {{!}} std::views::filter(odd)) | ||
std::cout << value << ' '; | std::cout << value << ' '; | ||
+ | std::cout << '\n'; | ||
} | } | ||
|output= | |output= | ||
− | + | pair::second values in the list: 11.1 22.2 33.3 | |
+ | odd values in the map: 1 3 5 | ||
}} | }} | ||
+ | |||
+ | ===Defect reports=== | ||
+ | {{dr list begin}} | ||
+ | {{dr list item|wg=lwg|dr=3563|std=C++20|before={{tt|keys_view}} is unable to participate in CTAD due to its use of {{lc|views::all_t}}|after={{lc|views::all_t}} removed}} | ||
+ | {{dr list end}} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/ranges/dsc keys_view}} | + | {{dsc inc|cpp/ranges/dsc keys_view}} |
− | {{dsc inc | cpp/ranges/dsc elements_view}} | + | {{dsc inc|cpp/ranges/dsc elements_view}} |
− | {{dsc inc | cpp/numeric/valarray/dsc slice}} | + | {{dsc inc|cpp/numeric/valarray/dsc slice}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|de|es|fr|it|ja|pt|ru|zh}} | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 12:48, 20 October 2023
Defined in header <ranges>
|
||
template< class R > using values_view = ranges::elements_view<R, 1>; |
(1) | (since C++20) |
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.
Contents |
[edit] 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
[edit] 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
[edit] 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 due to its use of views::all_t
|
views::all_t removed |
[edit] See also
(C++20) |
takes a view consisting of pair-like values and produces a view of the first elements of each pair(class template) (range adaptor object) |
takes a view consisting of tuple-like values and a number N and produces a view of Nth element of each tuple(class template) (range adaptor object) | |
BLAS-like slice of a valarray: starting index, length, stride (class) |