std::get(std::pair)

From cppreference.com
< cpp‎ | utility‎ | pair
(1)
template< size_t N, 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 N, 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 N, 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 N, 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 N, 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 N, 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 <class T, class U>

constexpr T& get(std::pair<T, U>& p);

template <class T, class U>

constexpr const T& get(const std::pair<T, U>& p);
(4) (since C++14)
template <class T, class U>
constexpr T&& get(std::pair<T, U>&& p);
(5) (since C++14)
template <class T, class U>

constexpr T& get(std::pair<U, T>& p);

template <class T, class U>

constexpr const T& get(const std::pair<U, T>& p);
(6) (since C++14)
template <class T, class U>
constexpr T&& get(std::pair<U, T>&& p);
(7) (since C++14)

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

Type-based overloads 4-7 fail to compile if the types T and U are the same.

Contents

[edit] Parameters

p - pair whose contents to extract

[edit] Return value

1,2) Returns p.first if N==0 and p.second if N==1.
3) Returns std::forward<T1&&>(p.first) if N==0 and std::forward<T2&&>(p.second) if N==1
4) Returns std::get<0>(p)
5) Returns std::get<0>(std::move(p))
6) Returns std::get<1>(p)
7) Returns std::get<1>(std::move(p))

[edit] Exceptions

noexcept specification:  
noexcept
  

[edit] Example

#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

tuple accesses specified element
(function template)
accesses an element of an array
(function template)