函式模板
<utility>

std::make_pair

template <class T1, class T2>  pair<T1,T2> make_pair (T1 x, T2 y);
template <class T1, class T2>  pair<V1,V2> make_pair (T1&& x, T2&& y);  // see below for definition of V1 and V2
構造 pair 物件
構造一個 pair 物件,將其第一個元素設定為 x,第二個元素設定為 y

模板型別可以從傳遞給 make_pair 的引數中隱式推匯出來。

pair 物件可以從包含不同型別的其他 pair 物件構造,如果相應的型別可以隱式轉換。

此函式模板的行為與如下定義相同:
1
2
3
4
5
template <class T1,class T2>
  pair<T1,T2> make_pair (T1 x, T2 y)
  {
    return ( pair<T1,T2>(x,y) );
  }
函式返回
1
pair<V1,V2>(std::forward<T1>(x),std::forward<T2>(y))
其中 V1V2 分別是 T1T2decay 等價物(除了 reference_wrapper 型別,它們會使用相應的引用型別代替)。

如果 T1 和/或 T2右值引用,則物件會被移動,而 x 和/或 y 將處於未定義但有效狀態。

引數

x, y
值,分別賦給正在構造的 pair 物件的 firstsecond 成員。

返回值

一個 pair 物件,其 firstsecond 元素分別設定為 xy
模板引數可以被隱式推導。

示例

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

int main () {
  std::pair <int,int> foo;
  std::pair <int,int> bar;

  foo = std::make_pair (10,20);
  bar = std::make_pair (10.5,'A'); // ok: implicit conversion from pair<double,char>

  std::cout << "foo: " << foo.first << ", " << foo.second << '\n';
  std::cout << "bar: " << bar.first << ", " << bar.second << '\n';

  return 0;
}

輸出

foo: 10, 20
bar: 10, 65


資料競爭

如果 T1T2 (或兩者) 是支援移動語義的型別的右值引用型別,則其相應的引數會被修改。

異常安全

該函式為每個物件提供與相應的元素建構函式相同的保證級別。

另見