Namespaces
Variants
Views
Actions

std::make_pair

From cppreference.com
< cpp‎ | utility‎ | pair
Revision as of 08:32, 26 January 2013 by SuperBoi45 (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <utility>
</td>

<td></td> <td></td> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
template< class T1, class T2 >

std::pair<T1,T2> make_pair( T1 t, T2 u );
template< class T1, class T2 >

std::pair<V1,V2> make_pair( T1&& t, T2&& u );
</td>

<td class="t-dcl-nopad"> </td> <td > (until C++11)

(since C++11) </td> </tr> Template:ddcl list end

Creates a Template:cpp/ltt object, deducing the target type from the types of arguments.

The deduced types are std::decay<T1>::type and std::decay<T2>::type (the usual type transformations applied to arguments of functions passed by value) unless application of std::decay results in std::reference_wrapper<X> for some type X, in which case the deduced type is X&. (since C++11)

Parameters

t, u - the values to construct the pair from

Return value

an Template:cpp/ltt object containing the given values.

Example

#include <iostream>
#include <utility>
#include <functional>
 
int main()
{
    int n = 1;
    int a[5] = {1, 2, 3, 4, 5};
 
    // build a pair from two ints
    auto p1 = std::make_pair(n, a[1]);
    std::cout << "The value of p1 is "
              << "(" << p1.first << ", " << p1.second << ")\n";
 
    // build a pair from a reference to int and an array (decayed to pointer)
    auto p2 = std::make_pair(std::ref(n), a);
    n = 7;
    std::cout << "The value of p2 is "
              << "(" << p2.first << ", " << *(p2.second + 1) << ")\n";
}

Output:

The value of p1 is (1, 2)
The value of p2 is (7, 2)