public member function (公共成员函数)
<utility>

std::pair::pair (std::::对)

默认 (1)
pair();
复制 (2)
template<class U, class V> pair (const pair<U,V>& pr);
initialization (3) (初始化 (3))
pair (const first_type& a, const second_type& b);
默认 (1)
constexpr pair();
copy / move (2) (复制/移动 (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) (初始化 (3))
pair (const first_type& a, const second_type& b);template<class U, class V> pair (U&& a, V&& b);
piecewise (4) (分段 (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. (构造一个pair对象。)
This involves individually constructing its two component objects, with an initialization that depends on the constructor form invoked (这涉及到单独构造它的两个组成对象,其初始化取决于调用的构造函数形式)

(1) 默认构造函数
Constructs a pair object with its elements value-initialized. (构造一个pair对象,其元素进行值初始化。)
(2) copy / move constructor (and implicit conversion) ((2) 复制/移动构造函数(和隐式转换))
The object is initialized with the contents of the pr pair object. (该对象使用pr pair对象的内容进行初始化。)
The corresponding member of pr is passed to the constructor of each of its members. (pr的相应成员被传递给其每个成员的构造函数。)
(3) initialization constructor ((3) 初始化构造函数)
Member first is constructed with a and member second with b. (成员first使用a构造,成员second使用b构造。)
(4) piecewise constructor ((4) 分段构造函数)
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. (就地构造成员firstsecond,将first_args的元素作为参数传递给first的构造函数,并将second_args的元素传递给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). (大多数形式都有两个签名:一个采用const 左值引用,它将值复制到pair中,另一个采用右值引用,如果它们的类型支持移动语义,它会移动它们(对于这种类型,内容被转移到pair对象,并被它们之前的引用者丢失,它们处于未指定但有效的状态)。)

参数

pr
Another pair object. (另一个pair对象。)
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. (这可以是与正在构造的对象相同类型的对象,也可以是pair类型,其元素的类型可以隐式转换为正在构造的pair中的类型。)
a
An object of the type of first, or some other type implicitly convertible to it. (类型为first的对象,或可隐式转换为它的其他类型。)
和 b
An object of the type of second, or some other type implicitly convertible to it. (类型为second的对象,或可隐式转换为它的其他类型。)
pwc
The piecewise_construct object. (piecewise_construct对象。)
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. (tuple对象,其中包含要传递给成员firstsecond的构造函数的参数。)

示例

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;
}

输出
The price of lightbulbs is $0.99
The price of shoes is $39.9
The price of tomatoes is $2.3


数据竞争

The elements of pr, first_args and second_args are accessed. (访问prfirst_argssecond_args的元素。)
The constructors taking rvalue references as arguments modify these arguments if their types support move semantics for this construction. (如果它们的类型支持此构造的移动语义,则采用右值引用作为参数的构造函数会修改这些参数。)

异常安全

If none of the individual constructions of members of pair can throw, the operation never throws exceptions (no-throw guarantee). (如果pair成员的任何单个构造都不会抛出异常,则该操作永远不会抛出异常(无抛出保证)。)
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). (否则,如果调用任何采用右值引用作为参数的形式,并且pair中的至少一种类型可以使用移动语义构造,则该操作可能会在发生异常时使pr处于无效状态(不保证)。)
Otherwise, the function only implies copies and the operation produces no side effects (strong guarantee). (否则,该函数仅表示复制,并且该操作不会产生副作用(强保证)。)

另见