inline constexpr auto keys = ranges::elements<0>;
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.
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[edit] Example
Displays values for each type of quark in particle physics.
#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:
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 â[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++20keys_view
is unable to participate in CTAD due to its use of views::all_t views::all_t removed [edit] See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4