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

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

问题描述

std :: make_pair 的目的是什么?

为什么不只是 std :: pair< int,char>(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天全站免登陆