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 的构造函数的目的是什么?


- C++ 协变模板 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- STL 中有 dereference_iterator 吗? 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 近似搜索的工作原理 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 静态初始化顺序失败 2022-01-01
- 从python回调到c++的选项 2022-11-16