Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::views::keys,std::ranges::keys_view

      From cppreference.com
      <cpp‎ |ranges
       
       
      Ranges library
      Range adaptors
       
      Defined in header<ranges>
      template<class R>
      using keys_view=ranges::elements_view<R,0>;
      (1)(since C++20)
      namespace views{

         inlineconstexprauto keys= ranges::elements<0>;

      }
      (2)(since C++20)

      Takes aview oftuple-like values (e.g.std::tuple orstd::pair), and produces a view with avalue-type of thefirst element of the adapted view's value-type.

      1) An alias forranges::elements_view<R,0>.
      2)RangeAdaptorObject (and alsoRangeAdaptorClosureObject). The expressionviews::keys(e) isexpression-equivalent tokeys_view<views::all_t<decltype((e))>>{e} for any suitable subexpressione.

      Contents

      [edit]Notes

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

      std::map<std::string,int> map{{"one",1},{"two",2}}; for(autoconst& key: std::views::keys(map))std::cout<< key<<' ';// prints: one two

      [edit]Example

      Displays values for each type ofquark in particle physics.

      Run this code
      #include <iomanip>#include <iostream>#include <locale>#include <ranges>#include <string>#include <tuple>#include <vector> int main(){conststd::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::stringconst& name: std::views::keys(quark_mass_charge))std::cout<<std::setw(9)<< name<<" │ "; std::cout<<"\n""Mass MeV/c²: │ ";for(constdouble mass: std::views::values(quark_mass_charge))std::cout<<std::setw(9)<< mass<<" │ "; std::cout<<"\n""E-charge:    │ ";for(constbool 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.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3563C++20keys_view is unable to participate in CTAD due to its use ofviews::all_tviews::all_t removed

      [edit]See also

      takes aview consisting of pair-like values and produces aview of the second elements of each pair
      (class template)(range adaptor object)[edit]
      takes aview consisting oftuple-like values and a number N and produces aview of Nth element of each tuple
      (class template)(range adaptor object)[edit]
      BLAS-like slice of a valarray: starting index, length, stride
      (class)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/keys_view&oldid=160902"

      [8]ページ先頭

      ©2009-2025 Movatter.jp