C++STL库中map容器常用应用

#include<iostream>
#include<cstdio>
#include<map> //按键值大小构成二叉搜索树 
using namespace std;
map<int, string> a;
int main()
{
    a.insert(map<int, string>::value_type(1,"li"));
    a.insert(map<int, string>::value_type(1,"LI"));//键值存在,插入失败 
    a.insert(pair<int, string>(2, "yang"));
    a.insert(pair<int, string>(2, "YANG"));//键值存在,插入失败 
    a[3]="wang";
    a[3]="WANG";//键值存在,进行覆盖 
    a.insert(make_pair(4,"dong"));
    a.insert(make_pair(4,"DONG"));//键值存在,插入失败 
    map<int, string>::iterator it;
    for(it=a.begin(); it!=a.end(); it++)
    {
        cout<<it->first<<"   "<<it->second<<endl;
    }

    if(a.find(1)!=a.end())
    {
        cout<<"find success!"<<endl;
    }
    else
    {
        cout<<"losing finding!"<<endl;
    }
    
    if(a.count(1)==true)
    {
        cout<<"find success!"<<endl;
    }
    else
    {
        cout<<"losing find! "<<endl;
    }
    cout<<"map中元素个数为"<<a.size()<<endl;
    a.erase(a.begin(),a.end());
    if(a.empty())
    {
        cout<<"map is empty"<<endl;
    }
    else
    {
        cout<<"map is not empty"<<endl;
    }
    return 0;
}

 

posted on 2015-08-30 13:01  vCoders  阅读(209)  评论(0)    收藏  举报

导航