c++ map容器用法

map容器

map:是一種鍵值對的容器,特色,查找很是的快,元素不能重複。
使用以前#include ,map與python中的字典有殊途同歸之妙。
話很少說,直接代碼調試。
python

map<int ,char>  mapp;
    cout<<mapp.size();
    cout<<mapp.empty();
    mapp[1001]='m';//給鍵值對的鍵對應的值賦值
    mapp[1002]='w';
    mapp[1003]='m';
    cout<<mapp[1002];//取值。
    mapp.erase(1002);//刪除key爲1002的值。
    mapp.erase(mapp.begin());//刪除的位置,必須爲迭代器型。
    cout<<mapp.size();



    cout<<"      ";
    mapp[1005]='w';
    mapp[1006]='m';
    mapp[1007]='w';
    mapp[1008]='m';
    map<int ,char>::iterator itor1,itor2;
    itor1=mapp.begin();
    itor2=mapp.end();
    cout<<endl;
    for(itor1;itor1!=itor2;itor1++)
    {
        int k=itor1->first;
        char v=itor1->second;
        cout<<k<<":"<<v<<endl;
        //cout<<*itor1<<"   ";
    }
    cout<<endl;
相關文章
相關標籤/搜索