标签:Right make C++ second pair Ty2 first
一、相同点
pair和make_pair的主要作用都是将两个数据组合成一个数据,两个数据可以是同一个类型或者不同类型。
二、区别
- pair实际上是一个结构体,其主要的两个成员变量是first和second,这两个变量可以直接使用。
伪代码如下:
first = pair.first
second= pair.second
- 一般make_pair都使用在需要pair做参数的位置,可以直接调用make_pair生成pair对象。
伪代码如下:
pair <string,double> product3;
product3 = make_pair ("shoes",20.0);
三、 pair和make_pair应用实例
#include <iostream>
#include <utility>
#include <string>
using namespace std;
int main () {
pair <string,double> product1 ("tomatoes",3.25);
pair <string,double> product2;
pair <string,double> product3;
product2.first = "lightbulbs"; // type of first is string
product2.second = 0.99; // type of second is double
product3 = make_pair ("shoes",20.0);
cout << "The price of " << product1.first << " is $" << product1.second << "\n";
cout << "The price of " << product2.first << " is $" << product2.second << "\n";
cout << "The price of " << product3.first << " is $" << product3.second << "\n";
return 0;
}
四、pair和make_pair定义
// TEMPLATE STRUCT pair
template<class _Ty1,class _Ty2> struct pair
{ // store a pair of values
typedef pair<_Ty1, _Ty2> _Myt;
typedef _Ty1 first_type;
typedef _Ty2 second_type;
pair(): first(_Ty1()), second(_Ty2()){
// construct from defaults
}
pair(const _Ty1& _Val1, const _Ty2& _Val2): first(_Val1), second(_Val2){
// construct from specified values
}
template<class _Other1,
class _Other2>
pair(const pair<_Other1, _Other2>& _Right)
: first(_Right.first), second(_Right.second){
// construct from compatible pair
}
void swap(_Myt& _Right){
// exchange contents with _Right
std::swap(first, _Right.first);
std::swap(second, _Right.second);
}
_Ty1 first; // the first stored value
_Ty2 second; // the second stored value
};
template<class _Ty1,class _Ty2> inline
pair<_Ty1, _Ty2> make_pair(_Ty1 _Val1, _Ty2 _Val2){
// return pair composed from arguments
return (pair<_Ty1, _Ty2>(_Val1, _Val2));
}
标签:Right,make,C++,second,pair,Ty2,first 来源: https://blog.csdn.net/LiuXF93/article/details/121116348
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。