欢迎来到CTHRVAD的博客

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。
扩大
缩小

map 函数

map 函数的使用方法 比较好使用

关键词 键值对 大小 第一个 最后一个 map

#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
    map<string,int> m;// create the map m 键值对 string int
    m["hello"]=2;
    cout<<m["hello"]<<endl; //输出 键值对的数值

    cout<<m["world"]<<endl;// 此时键值对不存在,看看有什么结果 输出0

    m["world"]=3; //创建键值对 world

    m[","]=1;//创建1看看会发生什么

    //迭代输出 键 用 first 获取 值用second获取
    for(auto it=m.begin();it!=m.end();it++)
    {
        cout<<it->first<<"  "<<it->second<<endl; //此时输出按照值的大小由小到大排列
    }
    //输出第一个元素 begin
    cout<<m.begin()->first<<" "<<m.begin()->second<<endl;
    // 输出最后一个元素 rbegin
    cout<<m.rbegin()->first<<" "<<m.rbegin()->second<<endl;
    //输出空间大小
    cout<<m.size()<<endl; //大小为3







return 0;
}

posted on 2018-11-07 22:07  cthrvad  阅读(298)  评论(0)    收藏  举报

导航