#include<iostream>
#include<string>
#include <utility>
#include <map>
using namespace std;
int main()
{
pair<string,string> exch ;
typedef pair<string,string> p;
string first = "hello",last = "world",last2 = "new world";
exch = make_pair(first,last);//pair类型初始方式一
p exch2(first,last2),exch3;//pair类型初始方式二
exch3.first = "hi";exch3.second = "how do you do";//pair类型初始方式三
cout<<exch.first<<"\t"<<exch.second<<endl;
cout<<exch2.first<<"\t"<<exch2.second<<endl;
cout<<exch3.first<<"\t"<<exch3.second<<endl;
map<string ,int> mem;
//typedef pair<string,int> ptype;
typedef map<string,int>::value_type ptype;//equivalent to upper
mem["A"] = 1;
mem["A"] = 2;//覆盖上面的值
pair<map<string,int>::iterator,bool> a = mem.insert(ptype("A",3));//不会覆盖上面的值,key存在,不能insert
cout<<mem["A"]<<endl;
cout<< a.first->first <<"\t"<< a.first->second
<<"\t"<<a.second<<endl;
while(1);
return 0;
}
pair类型 map
最新推荐文章于 2023-05-04 10:25:04 发布