std::ranges::views::keys, std::ranges::keys_view
Min standard notice:
Header: <ranges>
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.
# Declarations
template< class R >
using keys_view = ranges::elements_view<R, 0>;
(since C++20)
namespace views {
inline constexpr auto keys = ranges::elements<0>;
}
(since C++20)
# Notes
keys_view can be useful for extracting keys from associative containers, e.g.
# Example
#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';
}
# Defect reports
| 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 |