Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::get(std::pair)

      From cppreference.com
      <cpp‎ |utility‎ |pair
       
       
      Utilities library
       
       
      Defined in header<utility>
      template<std::size_t I,class T1,class T2>

      typenamestd::tuple_element<I,std::pair<T1,T2>>::type&

          get(std::pair<T1, T2>& p)noexcept;
      (1)(since C++11)
      (constexpr since C++14)
      template<std::size_t I,class T1,class T2>

      consttypenamestd::tuple_element<I,std::pair<T1,T2>>::type&

          get(conststd::pair<T1,T2>& p)noexcept;
      (2)(since C++11)
      (constexpr since C++14)
      template<std::size_t I,class T1,class T2>

      typenamestd::tuple_element<I,std::pair<T1,T2>>::type&&

          get(std::pair<T1,T2>&& p)noexcept;
      (3)(since C++11)
      (constexpr since C++14)
      template<std::size_t I,class T1,class T2>

      consttypenamestd::tuple_element<I,std::pair<T1,T2>>::type&&

          get(conststd::pair<T1,T2>&& p)noexcept;
      (4)(since C++11)
      (constexpr since C++14)
      template<class T,class U>
      constexpr T& get(std::pair<T, U>& p)noexcept;
      (5)(since C++14)
      template<class T,class U>
      constexprconst T& get(conststd::pair<T, U>& p)noexcept;
      (6)(since C++14)
      template<class T,class U>
      constexpr T&& get(std::pair<T, U>&& p)noexcept;
      (7)(since C++14)
      template<class T,class U>
      constexprconst T&& get(conststd::pair<T, U>&& p)noexcept;
      (8)(since C++14)
      template<class T,class U>
      constexpr T& get(std::pair<U, T>& p)noexcept;
      (9)(since C++14)
      template<class T,class U>
      constexprconst T& get(conststd::pair<U, T>& p)noexcept;
      (10)(since C++14)
      template<class T,class U>
      constexpr T&& get(std::pair<U, T>&& p)noexcept;
      (11)(since C++14)
      template<class T,class U>
      constexprconst T&& get(conststd::pair<U, T>&& p)noexcept;
      (12)(since C++14)

      Extracts an element from the pair usingtuple-like interface.

      1-4) The index-based overloads fail to compile if the indexI is neither0 nor1.
      5-12) The type-based overloads fail to compile if the typesT andU are the same.

      Contents

      [edit]Parameters

      p - pair whose contents to extract

      [edit]Return value

      1-4) Returns a reference top.first ifI==0 and a reference top.second ifI==1.
      5-8) Returns a reference top.first.
      9-12) Returns a reference top.second.

      [edit]Example

      Run this code
      #include <iostream>#include <utility> int main(){auto p=std::make_pair(1,3.14);std::cout<<'('<< std::get<0>(p)<<", "<< std::get<1>(p)<<")\n";std::cout<<'('<< std::get<int>(p)<<", "<< std::get<double>(p)<<")\n";}

      Output:

      (1, 3.14)(1, 3.14)

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2485C++11 (by index)
      C++14 (by type)
      there are no overloads for const pair&&the overloads are added

      [edit]See also

      Structured binding(C++17) binds the specified names to sub-objects or tuple elements of the initializer[edit]
      tuple accesses specified element
      (function template)[edit]
      accesses an element of anarray
      (function template)[edit]
      reads the value of the variant given the index or the type (if the type is unique), throws on error
      (function template)[edit]
      obtains iterator or sentinel from astd::ranges::subrange
      (function template)[edit]
      obtains a reference to real or imaginary part from astd::complex
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/pair/get&oldid=175379"

      [8]ページ先頭

      ©2009-2025 Movatter.jp