pair类型 map

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值