What is the purpose of std::make_pair vs the constructor of std::pair?(std::make_pair 与 std::pair 的构造函数的目的是什么?)
问题描述
std::make_pair 的目的是什么?
为什么不直接做 std::pair?
这两种方法有什么区别吗?
Is there any difference between the two methods?
推荐答案
区别在于 std::pair 需要指定两个元素的类型,而 std::make_pair 将创建一个带有传递给它的元素类型的对,而无需您告诉它.无论如何,这就是我可以从各种文档中收集到的信息.
The difference is that with std::pair you need to specify the types of both elements, whereas std::make_pair will create a pair with the type of the elements that are passed to it, without you needing to tell it. That's what I could gather from various docs anyways.
从 http://www.cplusplus.com/reference/std 中查看此示例/utility/make_pair/
pair <int,int> one;
pair <int,int> two;
one = make_pair (10,20);
two = make_pair (10.5,'A'); // ok: implicit conversion from pair<double,char>
除了它的隐式转换奖励,如果你没有使用 make_pair 你就必须这样做
Aside from the implicit conversion bonus of it, if you didn't use make_pair you'd have to do
one = pair<int,int>(10,20)
每次分配给一个,时间久了会很烦...
every time you assigned to one, which would be annoying over time...
这篇关于std::make_pair 与 std::pair 的构造函数的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:std::make_pair 与 std::pair 的构造函数的目的是什么?
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
