std::make_pair 与 std::pair 的构造函数的目的是什么? [英] What is the purpose of std::make_pair vs the constructor of std::pair?

查看:27
本文介绍了std::make_pair 与 std::pair 的构造函数的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std::make_pair 的目的是什么?

为什么不直接做 std::pair(0, 'a')?

这两种方法有什么区别吗?

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 的构造函数的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆