1、pair(头文件<utility>)
typedef pair<string,double> P; P p1("Kite",1.2); P p2("Tom",3.2);
P p3;
p3 = p1; //直接赋值
p1 == p2; //即p1,p2的第一二个元素对应相等
p1 < p2; //遵循字典序比较p1和p2的第一二个元素
#include<utility> #include<iostream> using namespace std; pair<string,int> f(int a){ return make_pair("Tom",a); } int main(){ string name; int grade; tie(name,grade) = f(99); cout << name << ‘ ‘ << grade; return 0; }
2、map(头文件<map>)
#include<iostream> #include<map> #include<algorithm> using namespace std; map<int,string>m; int main(){ m[2] = "Kite"; m[1] = "Tom"; m[4] = "Jim"; m[3] = "Mark"; for(auto it = m.begin();it != m.end();it++){ cout << it->first << ‘:‘ << it->second << endl; //map内部是按照first进行排序的 //m.size()返回map的大小 } return 0; }
原文:https://www.cnblogs.com/Cmathe/p/12260219.html