aptorObject") (and also [ RangeAdaptorClosureObject
](/w/cpp/named_req/RangeAdaptorClosureObject "cpp/named
req/RangeAdaptorClosureObject") ). The expression  views  ::  keys  (  e  )
is [ expression-equivalent ](/w/cpp/language/expressions#Expression-
equivalence "cpp/language/expressions") to  keys_view  < [ views::  all_t
](http://en.cppreference.com/w/cpp/ranges/all_view) < decltype  (  (  e  )  )
>> {  e  }  for any suitable subexpression  e  .

##  Contents

  * 1  Notes 
  * 2  Example 
  * 3  Defect reports 
  * 4  See also 

  
---  
  
###  [ [ edit
](/mwiki/index.php?title=cpp/ranges/keys_view&action=edit&section=1 "Edit
section: Notes") ]  Notes

keys_view  can be useful for extracting _keys_ from associative containers,
e.g.

    
    
    [std::map](http://en.cppreference.com/w/cpp/container/map)<[std::string](http://en.cppreference.com/w/cpp/string/basic_string), int> map{{"one", 1}, {"two", 2}};
     
    for (auto const& key : std::views::keys(map))
        [std::cout](http://en.cppreference.com/w/cpp/io/cout) << key << ' ';
    // prints: one two

###  [ [ edit
](/mwiki/index.php?title=cpp/ranges/keys_view&action=edit&section=2 "Edit
section: Example") ]  Example

Displays values for each type of [ quark ](https://en.wikipedia.org/wiki/quark
"enwiki:quark") in particle physics.

Run this code

    
    
    #include <iomanip>
    #include <iostream>
    #include <locale>
    #include <ranges>
    #include <string>
    #include <tuple>
    #include <vector>
     
    int main()
    {
        const [std::vector](http://en.cppreference.com/w/cpp/container/vector)<[std::tuple](http://en.cppreference.com/w/cpp/utility/tuple)<[std::string](http://en.cppreference.com/w/cpp/string/basic_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](http://en.cppreference.com/w/cpp/io/cout).imbue([std::locale](http://en.cppreference.com/w/cpp/locale/locale)("en_US.utf8"));
        [std::cout](http://en.cppreference.com/w/cpp/io/cout) << "Quark name:  │ ";
        for ([std::string](http://en.cppreference.com/w/cpp/string/basic_string) const& name : std::views::keys(quark_mass_charge))
            [std::cout](http://en.cppreference.com/w/cpp/io/cout) << [std::setw](http://en.cppreference.com/w/cpp/io/manip/setw)(9) << name << " │ ";
     
        [std::cout](http://en.cppreference.com/w/cpp/io/cout) << "\n" "Mass MeV/c²: │ ";
        for (const double mass : std::[views::values](http://en.cppreference.com/w/cpp/ranges/values_view)(quark_mass_charge))
            [std::cout](http://en.cppreference.com/w/cpp/io/cout) << [std::setw](http://en.cppreference.com/w/cpp/io/manip/setw)(9) << mass << " │ ";
     
        [std::cout](http://en.cppreference.com/w/cpp/io/cout) << "\n" "E-charge:    │ ";
        for (const bool pos : std::[views::elements](http://en.cppreference.com/w/cpp/ranges/elements_view)<2>(quark_mass_charge))
            [std::cout](http://en.cppreference.com/w/cpp/io/cout) << [std::setw](http://en.cppreference.com/w/cpp/io/manip/setw)(9) << (pos ? "+2/3" : "-1/3") << " │ ";
        [std::cout](http://en.cppreference.com/w/cpp/io/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
](/mwiki/index.php?title=cpp/ranges/keys_view&action=edit&section=3 "Edit
section: Defect reports") ]  Defect reports

The following behavior-changing defect reports were applied retroactively to
previously published C++ standards.

DR  |  Applied to  |  Behavior as published  |  Correct behavior   
---|---