map用法

来源:互联网 发布:万网独立域名 编辑:程序博客网 时间:2024/06/15 15:33
#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;    }}
原创粉丝点击