std::make_pair
来自 cppreference.cn
定义于头文件 <utility> |
||
template< class T1, class T2 > std::pair<T1, T2> make_pair( T1 x, T2 y ); |
(C++11 前) | |
template< class T1, class T2 > std::pair</*V1*/, /*V2*/> make_pair( T1&& x, T2&& y ); |
(C++11 起) (constexpr C++14 起) (C++20 前) |
|
template< class T1, class T2 > constexpr std::pair<std::unwrap_ref_decay_t<T1>, |
(C++20 起) | |
创建 std::pair 对象,从实参类型推导目标类型。
给定类型 std::decay<T1>::type 为
|
(C++11 起) (C++20 前) |
目录 |
[编辑] 参数
x, y | - | 用于构造 pair 的值 |
[编辑] 返回值
std::pair<T1, T2>(x, y) |
(C++11 前) |
std::pair</*V1*/, /*V2*/>(std::forward<T1>(x), std::forward<T2>(y)) |
(C++11 起) (C++20 前) |
std::pair<std::unwrap_ref_decay_t<T1>, std::unwrap_ref_decay_t<T2>> |
(C++20 起) |
[编辑] 示例
运行此代码
#include <functional> #include <iostream> #include <utility> 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 + 2) << ")\n"; }
输出
The value of p1 is (1, 2) The value of p2 is (7, 3)
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 181 | C++98 | 形参类型为 const 引用 类型,这使得传递数组成为不可能 |
将这些类型更改 为值类型 |