std :: make_pair的用途 [英] Purpose of std::make_pair

查看:204
本文介绍了std :: make_pair的用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: make_pair ?的目的是什么?



为什么不是 std :: pair< int,char>(0,'a')



两种方法有什么区别? / p>

解决方案

不同之处在于 std :: pair 两个元素的类型,而 std :: make_pair 将创建一个与传递给它的元素的类型,而不需要告诉它。



http://www.cplusplus.com/reference/std/utility/make_pair/

  pair< int,int>一; 
pair< int,int>二;

one = make_pair(10,20);
two = make_pair(10.5,'A'); // ok:implicit conversion from pair< double,char>

除了隐式转换加成之外,如果你没有使用make_pair, do

  one = pair< int,int>(10,20)
/ pre>

每次分配给一个,这将是烦人的时间...


What is the purpose of std::make_pair?

Why not just do std::pair<int, char>(0, 'a')?

Is there any difference between the two methods?

解决方案

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.

See this example from 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>

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的用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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