std::ranges...:values_view(3)
NAME π
okay so welcome to std::ranges...:values_view(3) from the C++ Standard Libary β we're talking about std::ranges::views::values,std::ranges::values_view, and yes bestie, that comma is right there in the name. It's giving "grab the second element of tuple-like things" and honestly it ate. π
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)
}
So here's the tea, no cap: this 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. π«Ά
- It's an alias for
ranges::elements_view<R, 1>. - It's a
RangeAdaptorObject(and also aRangeAdaptorClosureObject). The expressionviews::values(e)is expression-equivalent tovalues_view<views::all_t<decltype((e))>>{e}for any suitable subexpressione.
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
That's lowkey iconic when you just want the values out of an associative container, iykyk. π¦
Example π₯
// Run this code β hit it, bestie:
#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. Not me being dramatic, this is literally the official list:
- DR:
LWG 3563 - Applied to: C++20
- Behavior as published:
keys_viewis unable to participate in CTAD due to its use ofviews::all_t - Correct behavior:
views::all_tremoved
See also π
ranges::keys_view/views::keys(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)ranges::elements_view/views::elements(C++20) β takes a view consisting of tuple-like values and a numberNand produces a view of N^th element of each tuple (class template) (range adaptor object)sliceβ BLAS-like slice of a valarray: starting index, length, stride (class)
mwah π β source/version: http://cppreference.com, date: 2024.06.10, and yes the page is still std::ranges...:values_view(3). That's the whole vibe. Bye bestie! π«Άπ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms