map 容器常用功能小结

具体功能参考:click here~~

#include <bits/stdc++.h>
using namespace std;
int main()
{
    /*map.at:修改元素值
    map<string,int>mm=
    {
        { "abc",0},
        { "bcd",0},
    };
    mm.at("abc")=-7;
    mm.at("bcd")=20;
    for(auto &x:mm)
    {
        cout<<x.first<<":"<<x.second<<endl;
    }
    output:
    abc -7
    bcd 20
    */

    /*map.erase:删除元素
    map<char,int>mm;
    map<char,int>::iterator it;
    mm['a']=10;
    mm['b']=13;
    mm['c']=12;
    it=mm.find('a');
    mm.erase(it);
    for(it=mm.begin();it!=mm.end();++it)
    {
        cout<<it->first<<" "<<it->second<<endl;
    }
    ouput:
    b 13;
    c 12;
    */

    /*删除连续一段数
    mm.lower_bound('b');
    mm.upper_bound('d');
    map<char,int>mm;
    map<char,int>::iterator it ,ita,itb;
    mm['a']=1;
    mm['b']=2;
    mm['c']=3;
    mm['d']=4;
    mm['e']=5;
    ita=mm.lower_bound('b');
    itb=mm.upper_bound('d');
    mm.erase(ita,itb);
    for(it=mm.begin();it!=mm.end();++it)
    cout<<it->first<<" "<<it->second<<endl;
    output:
    a 1;
    e 5;
    */

    /*map.max_size();最大值
    map<unsigned,unsigned>mm;
    cout<<mm.max_size()<<endl;
    output:
      mm.max_size()=134217727
    mm.max_size()=178956970
    */

    /* map.swap();//交换两个容器
     map<char,int>ma,mb;
     ma['x']=100;
     ma['y']=200;
     mb['a']=11;
     mb['b']=12;
     ma.swap(mb);
     for(map<char,int>::iterator it=ma.begin();it!=ma.end();++it)
     {
         cout<<it->first<<" "<<it->second<<endl;
     }
     for(map<char,int>::iterator it=mb.begin();it!=mb.end();++it)
     {
         cout<<it->first<<" "<<it->second<<endl;
     }
     output:
         a 11
         b 12
         x 100
         y 200
    */
}


你可能感兴趣的:(map,算法总结)