Namespaces
Variants
Views
Actions

std::pair::pair

From cppreference.com
< cpp‎ | utility‎ | pair
Revision as of 13:03, 2 November 2012 by P12bot (Talk | contribs)

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

<td >
pair();
constexpr pair();
</td>

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

<td >
pair( const T1& x, const T2& y );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class U1, class U2 >
pair( U1&& x, U2&& y );
</td>

<td > (3) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
template< class U1, class U2 >
pair( const pair<U1,U2>& p );
</td>

<td > (4) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class U1, class U2 >
pair( pair<U1,U2>&& p );
</td>

<td > (5) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
template< class... Args1, class... Args2 >

pair( std::piecewise_construct_t,
      std::tuple<Args1...> first_args,

      std::tuple<Args2...> second_args );
</td>

<td > (6) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
pair( const pair& p ) = default;
</td>

<td > (7) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
pair( pair&& p ) = default;
</td>

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


Constructs a new pair.

1) Default constructor. Value-initializes both elements of the pair, first and second.

2) Initializes first with x and second with y.

3) Initializes first with std::forward<U1>(x) and second with std::forward<U2>(y).

4) Initializes first with p.first and second with p.second.

5) Initializes first with std::move<U1>(p.first) and second with std::move<U2>(p.second).

6) Forwards the elements of first_args to the constructor of first and forwards the elements of second_args to the constructor of second. This is the only non-default constructor that can be used to create a pair of non-copyable non-movable types.

7) Copy constructor is implicitly generated.

8) Move constructor is implicitly generated.

Parameters

x - value to initialize the first element of this pair
y - value to initialize the second element of this pair
p - pair of values used to initialize both elements of this pair
first_args - tuple of constructor arguments to initialize the first element of this pair
second_args - tuple of constructor arguments to initialize the second element of this pair

Example

#include <utility>
#include <string>
#include <complex>
#include <tuple>
#include <iostream>
int main()
{
    std::pair<int, float> p1;
    std::cout << "Value-initialized: "
              << p1.first << ", " << p1.second << '\n';
 
    std::pair<int, double> p2(42, 0.123);
    std::cout << "Initialized with two values: "
              << p2.first << ", " << p2.second << '\n';
 
    std::pair<char, int> p4(p2);
    std::cout << "Implicitly converted: "
              << p4.first << ", " << p4.second << '\n';
 
    std::pair<std::complex<double>, std::string> p6(
                    std::piecewise_construct, 
                    std::forward_as_tuple(0.123, 7.7),
                    std::forward_as_tuple(10, 'a'));
    std::cout << "Piecewise constructed: "
              << p6.first << ", " << p6.second << '\n';
}

Output:

Value-initialized: 0, 0
Initialized with two values: 42, 0.123
Implicitly converted: *, 0
Piecewise constructed: (0.123,7.7), aaaaaaaaaa

See also

creates a pair object of type, defined by the argument types
(function template) [edit]