pair的使用
#include<bits/stdc++.h>
#include<set>
using namespace std;
int main()
{
pair<int,float>p1;
p1=make_pair(1,2.5);//pair 实例化
cout<<p1.first<<p1.second;
pair<int, float> p2(1, 1.2); //pair初始化
cout<< p2.first << endl;
cout<< p2.second << endl;
Map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, “student_one”)); //插入pair组
mapStudent.insert(pair<int, string>(2, “student_two”));
mapStudent.insert(pair<int, string>(3, “student_three”));
map<int, string>::iterator iter;
iter = mapStudent.find(1);
if(iter != mapStudent.end())
{
Cout<<"Find, the value is "<<iter->second<<endl;
}
Else
{
Cout<<"Do not Find"<<endl;
}
}
map &pair
自动建立Key - value的对应。key 和 value可以是任意你需要的类型。
根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。
#include<bits/stdc++.h>
#include<map>
using namespace std;
int main()
{
pair<int,int>p1[100];
map<int,int>m;
p1[0]=make_pair(1,4);
m.insert(p1[0]);
cout<<m[1]<<endl;
cout<<p1[0].first<<" "<<p1[0].second;
for(int i=0;i<5;i++)
{
int a,b;
cin>>a>>b;
p1[i].first=a;
p1[i].second=b;
m.insert(p1[i]);
}
cout<<m[5]; //默认为0,输入cout为1
cout<<m.count(5);
}