码迷,mamicode.com
首页 > 其他好文 > 详细

map 容器常用功能小结

时间:2015-06-19 13:30:54      阅读:149      评论:0      收藏:0      [点我收藏+]

标签: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 容器常用功能小结

标签:map   算法总结   

原文地址:http://blog.csdn.net/u013050857/article/details/46560441

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!