<utility>

pair

public member function
<utility>

std::pair::pair

default (1)
pair();
copy (2)
template<class U, class V> pair (const pair<U,V>& pr);
initialization (3)
pair (const first_type& a, const second_type& b);
default (1)
constexpr pair();
copy / move (2)
template<class U, class V> pair (const pair<U,V>& pr);
template<class U, class V> pair (pair<U,V>&& pr);
pair (const pair& pr) = default;
pair (pair&& pr) = default;
initialization (3)
pair (const first_type& a, const second_type& b);
template<class U, class V> pair (U&& a, V&& b);
piecewise (4)
template <class... Args1, class... Args2>
  pair (piecewise_construct_t pwc, tuple<Args1...> first_args,
                                   tuple<Args2...> second_args);
Construct pair
Constructs a pair object.
This involves individually constructing its two component objects, with an initialization that depends on the constructor form invoked:

(1) default constructor
Constructs a pair object with its elements value-initialized.
(2) copy / move constructor (and implicit conversion)
The object is initialized with the contents of the pr pair object.
The corresponding member of pr is passed to the constructor of each of its members.
(3) initialization constructor
Member first is constructed with a and member second with b.
(4) piecewise constructor
Constructs members first and second in place, passing the elements of first_args as arguments to the constructor of first, and the elements of second_args to the constructor of second.

Most forms have two signatures: one taking const lvalue references, which copies the values into the pair, and one taking rvalue references, which moves them instead if their types support move semantics (for such types, the contents are transferred to the pair object and lost by their previous referrers, which are left in an unspecified but valid state).

Parameters

pr
Another pair object.
This may be an object of the same type as the object being constructed or of a pair type whose elements' types are implicitly convertible to those in the pair being constructed.
a
An object of the type of first, or some other type implicitly convertible to it.
b
An object of the type of second, or some other type implicitly convertible to it.
pwc
The piecewise_construct object.
The only purpose of this argument is to select the proper constructor signature. It conveys no information to be incorporated into the new object.
first_args, second_args
tuple objects with the arguments to be passed to the constructors of members first and second.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// pair::pair example
#include <utility>      // std::pair, std::make_pair
#include <string>       // std::string
#include <iostream>     // std::cout

int main () {
  std::pair <std::string,double> product1;                     // default constructor
  std::pair <std::string,double> product2 ("tomatoes",2.30);   // value init
  std::pair <std::string,double> product3 (product2);          // copy constructor

  product1 = std::make_pair(std::string("lightbulbs"),0.99);   // using make_pair (move)

  product2.first = "shoes";                  // the type of first is string
  product2.second = 39.90;                   // the type of second is double

  std::cout << "The price of " << product1.first << " is $" << product1.second << '\n';
  std::cout << "The price of " << product2.first << " is $" << product2.second << '\n';
  std::cout << "The price of " << product3.first << " is $" << product3.second << '\n';
  return 0;
}


Output:
The price of lightbulbs is $0.99
The price of shoes is $39.9
The price of tomatoes is $2.3

Data races

The elements of pr, first_args and second_args are accessed.
The constructors taking rvalue references as arguments modify these arguments if their types support move semantics for this construction.

Exception safety

If none of the individual constructions of members of pair can throw, the operation never throws exceptions (no-throw guarantee).
Otherwise, if any of the forms taking an rvalue reference as argument is called, and at least one of the types in the pair can be constructed with move semantics, the operation may leave pr in an invalid state in case of exception (no guarantees).
Otherwise, the function only implies copies and the operation produces no side effects (strong guarantee).

See also