std::ranges...s::keys_view(3)
π std::ranges::views::keys,std::ranges::keys_view π
okay bestie, this is the C++ Standard Libary manual page for std::ranges::views::keys and std::ranges::keys_view β the manual name is std::ranges...s::keys_view(3), iykyk. It's the one that gives you a view of the first element of tuple-like values, and it is lowkey iconic for associative containers. No cap.
β¨ NAME β¨
std::ranges::views::keys,std::ranges::keys_view β yes, that's the whole name, comma and all, fr fr.
πΈ Synopsis πΈ
Defined in header <ranges>, and both of these are since C++20. Here's the synopsis tea:
Defined in header <ranges>
template< class R > (1) (since C++20)
using keys_view = ranges::elements_view<R, 0>;
namespace views {
inline constexpr auto keys = ranges::elements<0>; (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 first element of the adapted view's value-type.
- An alias for
ranges::elements_view<R, 0>. RangeAdaptorObject(and alsoRangeAdaptorClosureObject, double slay). The expressionviews::keys(e)is expression-equivalent tokeys_view<views::all_t<decltype((e))>>{e}for any suitable subexpressione.
π Notes π
keys_view can be useful for extracting keys from associative containers, e.g.
std::map<std::string, int> map{{"one", 1}, {"two", 2}};
for (auto const& key : std::views::keys(map))
std::cout << key << ' ';
// prints: one two
π Example π
Displays values for each type of quark in particle physics. The page has that // Run this code link, and here's the code it runs:
#include <iomanip>
#include <iostream>
#include <locale>
#include <ranges>
#include <string>
#include <tuple>
#include <vector>
int main()
{
const std::vector<std::tuple<std::string, double, bool>>
quark_mass_charge
{
// name, MeV/cΒ², has positive electric-charge:
{"up", 2.3, true}, {"down", 4.8, false},
{"charm", 1275, true}, {"strange", 95, false},
{"top", 173'210, true}, {"bottom", 4'180, false},
};
std::cout.imbue(std::locale("en_US.utf8"));
std::cout << "Quark name: β ";
for (std::string const& name : std::views::keys(quark_mass_charge))
std::cout << std::setw(9) << name << " β ";
std::cout << "\n" "Mass MeV/cΒ²: β ";
for (const double mass : std::views::values(quark_mass_charge))
std::cout << std::setw(9) << mass << " β ";
std::cout << "\n" "E-charge: β ";
for (const bool pos : std::views::elements<2>(quark_mass_charge))
std::cout << std::setw(9) << (pos ? "+2/3" : "-1/3") << " β ";
std::cout << '\n';
}
π¨οΈ Output π¨οΈ
The output is giving perfectly aligned columns, periodt:
Quark name: β up β down β charm β strange β top β bottom β
Mass MeV/cΒ²: β 2.3 β 4.8 β 1,275 β 95 β 173,210 β 4,180 β
E-charge: β +2/3 β -1/3 β +2/3 β -1/3 β +2/3 β -1/3 β
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- 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::values_view,views::values(C++20) β takes a view consisting of pair-like values and produces a view of the second 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 ofN^thelement of each tuple (class template) (range adaptor object).sliceβ BLAS-like slice of avalarray: starting index, length, stride (class).
xoxo, from the http://cppreference.com girlies β source/version 2024.06.10, page std::ranges...s::keys_view(3) πβ¨
π¦ 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