zoukankan      html  css  js  c++  java
  • c++ STL map简单使用

    map字典存放键值对

    内部组成是红黑树  查找 删除 插入复杂度为O(logn)

    初始化方式

    map<int,string>  str;

    插入方式

    1.使用pair 

    map<int ,string>str;
    str.insert(pair<int, string>(1, "one"));  

    2.value_type方式

    map<int,string>str;
    map.insert(map<int,string>::value_type(1,"one"));

    3.数组方式

    map<int,string>str;
    str[1] = "one";

    insert方式插入关键字存在,无法插入。

    使用数组可以覆盖关键字的值

    遍历时可使用反向迭代器遍历

    map<int, string>::reverse_iterator iter;  
    for(iter=str.rbegin();iter!=str.rend();++iter)
    {
            cout<<iter->first<<iter->second<<endl;
    }

    str.count(1);判断是否存在此键值对

    str.find(1) 返回此键值对迭代器位置

    swap 是交换两个map容器;

  • 相关阅读:
    Flutter 导航栏上添加搜索按钮
    tabController保活
    nav 选项卡
    flutter 毛玻璃
    [题解]CodeForces878 D
    [题解]CodeForces878C Tournament
    [题解]NOIP2012
    bzoj1070题解
    bzoj1061题解
    bzoj1059题解
  • 原文地址:https://www.cnblogs.com/9527s/p/13199534.html
Copyright © 2011-2022 走看看