std::make_pair
来自cppreference.com
![]() |
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <utility> 中定义
|
||
(至 C++11) (C++11 起) |
||
创建一个
std::pair
对象,推导出目标类型的参数类型.原文:
Creates a
std::pair
object, deducing the target type from the types of arguments.推导的类型是std::decay<T1>::type和std::decay<T2>::type(通常的类型转换施加到通过值的函数的参数),除非对于某些类型std::decay,在这种情况下,推导出的类型是std::reference_wrapper<X>
X
X&
结果应用。 (C++11 起)原文:
The deduced types are std::decay<T1>::type and std::decay<T2>::type (the usual type transformations applied to arguments of functions passed by value) unless application of std::decay results in std::reference_wrapper<X> for some type
X
, in which case the deduced type is is X&
. (C++11 起)[编辑] 参数
t, u | - | 值来构建对
原文: the values to construct the pair from |
[编辑] 返回值
[编辑] 示例
#include <iostream> #include <utility> #include <functional> 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+1) << ")\n"; }
输出:
The value of p1 is (1, 2) The value of p2 is (7, 2)