#include <map>
#include <iostream>
using namespace std;
int main()
{
map< string,int > a;
cout<<"插入:"<<endl;
a["April"]=112;//插入
cout<<a["April"]<<endl;
cout<<endl;
cout<<"查询:"<<endl;
map<string,int>::iterator pc;//查询一个元素是否在map里
pc=a.find("April");
if(pc==a.end()) cout<<"Not Find"<<endl;//如果找不到会自动返回“指向map尾部的迭代器”
else cout<<pc->second<<endl;//输出"April"对应的键值
cout<<endl;
cout<<"遍历"<<endl;
a["June"]=1;//遍历
a["July"]=2;
map<string,int>::iterator i;
for(i=a.begin(); i!=a.end(); i++)//不能等于a.end()
{
cout<<i->second<<endl;
}
cout<<endl;
cout<<"判断map是否是空集"<<endl;
cout<<a.empty()<<endl;//判断map是否是空集
cout<<endl;
cout<<"交换两个map的元素 "<<endl;
map<int ,int> b;//交换两个map的元素
map<int ,int> c;
c[1]=1;
c[2]=2;
b[1]=1000;
swap(b,c);
map<int,int> ::iterator j;
for(j=b.begin();j!=b.end();j++)
{
cout<<j->second<<endl;
}
for(j=c.begin();j!=c.end();j++)
{
cout<<j->second<<endl;
}
}
map用法
最新推荐文章于 2022-12-29 13:56:05 发布