##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::get(std::pair)

From cppreference.com
< cpp‎ | utility‎ | pair

Defined in header `<utility>` |  |
| (1) |
template< size_t I, class T1, class T2 > typename std::tuple_element<I, std::pair<T1,T2> >::type& get( pair<T1, T2>& p ); | (1) | (since C++11) (until C++14)
template< size_t I, class T1, class T2 > constexpr std::tuple_element_t<I, std::pair<T1,T2> >& get( pair<T1, T2>& p ); | (1) | (since C++14)
| (2) |
template< size_t I, class T1, class T2 > const typename std::tuple_element<I, std::pair<T1,T2> >::type& get( const pair<T1,T2>& p ); | (2) | (since C++11) (until C++14)
template< size_t I, class T1, class T2 > constexpr const std::tuple_element_t<I, std::pair<T1,T2> >& get( const pair<T1,T2>& p ); | (2) | (since C++14)
| (3) |
template< size_t I, class T1, class T2 > typename std::tuple_element<I, std::pair<T1,T2> >::type&& get( std::pair<T1,T2>&& p ); | (3) | (since C++11) (until C++14)
template< size_t I, class T1, class T2 > constexpr std::tuple_element_t<I, std::pair<T1,T2> >&& get( std::pair<T1,T2>&& p ); | (3) | (since C++14)
template< size_t I, class T1, class T2 > constexpr const std::tuple_element_t<I, std::pair<T1,T2> >&& get( const std::pair<T1,T2>&& p ); | (4) | (since C++17)
template <class T, class U> constexpr T& get(std::pair<T, U>& p); | (5) | (since C++14)
template <class T, class U> constexpr const T& get(const std::pair<T, U>& p); | (6) | (since C++14)
template <class T, class U> constexpr T&& get(std::pair<T, U>&& p); | (7) | (since C++14)
template <class T, class U> constexpr const T&& get(const std::pair<T, U>&& p); | (8) | (since C++17)
template <class T, class U> constexpr T& get(std::pair<U, T>& p); | (9) | (since C++14)
template <class T, class U> constexpr const T& get(const std::pair<U, T>& p); | (10) | (since C++14)
template <class T, class U> constexpr T&& get(std::pair<U, T>&& p); | (11) | (since C++14)
template <class T, class U> constexpr const T&& get(const std::pair<U, T>&& p); | (12) | (since C++17)

Extracts an element from the pair using tuple-like interface.

The index-based overloads (1-4) fail to compile if the index `I` is neither 0 nor 1.

The type-based overloads (5-12) fail to compile if the types `T` and `U` are the same.

### [edit] Parameters

p | - | pair whose contents to extract

### [edit] Return value

1-4) Returns a reference to `p.first` if `I==0` and a reference to `p.second` if `I==1`.
5-8) Returns a reference to `p.first`.
9-12) Returns a reference to `p.second`.

### [edit] Exceptions

`noexcept` specification:
noexcept

### [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] See also

std::get(std::tuple) | tuple accesses specified element (function template) [edit]
std::get(std::array) | accesses an element of an `array` (function template) [edit]
std::get(std::variant) (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]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/utility/pair/get&oldid=91671"
Category:

- unconditionally noexcept

Hidden categories:

- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template