pair 默认对first升序,当first相同时对second升序;
类模板:template <class T1, class T2> struct pair
参数:T1是第一个值的数据类型,T2是第二个值的数据类型。
功能:pair将一对值组合成一个值,这一对值可以具有不同的数据类型(T1和T2),两个值可以分别用pair的两个公有函数first和second访问。
具体用法:
1.定义(构造):
1 pairp1; //使用默认构造函数2 pair p2(1, 2.4); //用给定值初始化 3 pair p3(p2); //拷贝构造函数
2.访问两个元素(通过first和second):
1 pairp1; //使用默认构造函数2 p1.first = 1; 3 p1.second = 2.5; 4 cout << p1.first << ' ' << p1.second << endl;
输出结果:1 2.5
3.赋值operator = :
(1)利用make_pair:
1 pairp1;2 p1 = make_pair(1, 1.2);
(2)变量间赋值:
pairp1(1, 1.2); pair p2 = p1;
可以用cmp数组改;
代码:
1 #include2 #include 3 #include 4 #include 5 #include 6 #define mem(x,y) memset(x,y,sizeof(x)) 7 using namespace std; 8 typedef long long LL; 9 const int INF=0x3f3f3f3f;10 pair pa[100];11 int cmp(pair a,pair b){12 if(a.first!=b.first)return a.first>b.first;13 else return a.second