|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
(C++11) | ||||
| Non-member functions | ||||
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20) | ||||
(C++11) | ||||
get(std::pair) (C++11) | ||||
| Helper classes | ||||
(C++11) | ||||
(C++11) | ||||
(C++23) | ||||
(C++23) | ||||
(C++11) | ||||
| Deduction guides(C++17) |
Defined in header <utility> | ||
template<std::size_t I,class T1,class T2> typenamestd::tuple_element<I,std::pair<T1,T2>>::type& | (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& | (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&& | (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&& | (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.
I is neither0 nor1.T andU are the same.Contents |
| p | - | pair whose contents to extract |
#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)
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2485 | C++11 (by index) C++14 (by type) | there are no overloads for const pair&& | the overloads are added |
| Structured binding(C++17) | binds the specified names to sub-objects or tuple elements of the initializer[edit] |
(C++11) | tuple accesses specified element (function template)[edit] |
(C++11) | accesses an element of anarray(function template)[edit] |
(C++17) | reads the value of the variant given the index or the type (if the type is unique), throws on error (function template)[edit] |
(C++20) | obtains iterator or sentinel from astd::ranges::subrange (function template)[edit] |
(C++26) | obtains a reference to real or imaginary part from astd::complex (function template)[edit] |