std::ranges...s::keys_view(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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.

  1. An alias for ranges::elements_view<R, 0>.
  2. RangeAdaptorObject (and also RangeAdaptorClosureObject, double slay). The expression views::keys(e) is expression-equivalent to keys_view<views::all_t<decltype((e))>>{e} for any suitable subexpression e.

πŸŽ€ 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_view is unable to participate in CTAD due to its use of views::all_t. Correct behavior: views::all_t removed.

🌈 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 number N and 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).

xoxo, from the http://cppreference.com girlies β€” source/version 2024.06.10, page std::ranges...s::keys_view(3) πŸ’–βœ¨

πŸ“¦ where this page comes from