Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::views::values,std::ranges::values_view

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

         inlineconstexprauto values= ranges::elements<1>;

      }
      (2)(since C++20)

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

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

      Contents

      [edit]Notes

      values_view can be useful for extractingvalues from associative containers, e.g.

      std::map<int,std::string> map{{1,"alpha"},{2,"beta"}};for(autoconst& value: std::views::values(map))std::cout<< value<<' ';// prints: alpha beta

      [edit]Example

      Run this code
      #include <iostream>#include <map>#include <ranges> int main(){constauto list={std::pair{1,11.1},{2,22.2},{3,33.3}};std::cout<<"pair::second values in the list: ";for(double value: list| std::views::values)std::cout<< value<<' '; std::map<char,int> map{{'A',1},{'B',2},{'C',3},{'D',4},{'E',5}};auto odd=[](int x){return0!=(x&1);};std::cout<<"\nodd values in the map: ";for(int value: map| std::views::values| std::views::filter(odd))std::cout<< value<<' ';std::cout<<'\n';}

      Output:

      pair::second values in the list: 11.1 22.2 33.3odd values in the map: 1 3 5

      [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 first 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/values_view&oldid=160988"

      [8]ページ先頭

      ©2009-2025 Movatter.jp