map容器的用法:

map容器的用法:

 

#include "iostream"

#include "map"//map和multimap的头文件

using namespace std;

void test01()

{

    map<int,int>m;

    //插入方式

    m.insert(pair<int,int>(1,10));

    m.insert(make_pair(2,20));

    m.insert(map<int,int>::value_type(3,30));

    m[4]=40;

    for(map<int,int>::iterator it=m.begin();it!=m.end();it++){

        cout<<"Key:  "<<it->first<<"  Value:  "<<it->second<<"\n";

    }

}

void MyPrintf(map<int,int>&m)

{

    for(map<int,int>::iterator it=m.begin();it!=m.end();it++){

        cout<<"Key:  "<<it->first<<"  Value:  "<<it->second<<"\n";

    }

}

void test02()

{

    //clear(); 清空 find()查找 ,erase()删除;

    map<int,int>m;

    m.insert(make_pair(1,10));

    m.insert(make_pair(2,20));

    m.insert(make_pair(3,30));

    m.insert(make_pair(4,40));

    MyPrintf(m);

//    m.erase(3);//按key的值删除;

    MyPrintf(m);

    map<int,int>::iterator  res=m.find(4);

    if(res!=m.end()){

        cout<<"find"<<"  Key="<<res->first<<" value="<<res->second<<"\n";

    }else cout<<"No find"<<"\n";

    int num=m.count(4);

    cout<<"Key=4 map pair number: "<<num<<"\n";

    //lower_bound()与upper_bound();

    map<int,int>::iterator ret=m.lower_bound(3);

    if(ret!=m.end()){

        cout<<"Find"<<" lower_bound Key="<<ret->first<<"  value"<<ret->second<<"\n";

    }else cout<<"No find"<<"\n";

    ret=m.upper_bound(3);

    if(ret!=m.end()){

        cout<<"Find"<<" upper_bound Key="<<ret->first<<"  value"<<ret->second<<"\n";

    }else cout<<"No find"<<"\n";

    //equal_range()

    pair<map<int,int>::iterator,map<int,int>::iterator> it=m.equal_range(2);

    if(it.first!=m.end()){

        cout<<"lower_bound key:"<<it.first->first<<" value:"<<it.first->second<<" upper-bound key:"<<it.second->first<<" value:"<<it.second->second<<"\n";

    }

}

int main()

{

  // test01();

   test02();

    return 0;

}

map容器的用法:

上一篇:make 学习笔记


下一篇:音视频技术应用(3)-Linux编译x264,x265,fdk-aac