zoukankan      html  css  js  c++  java
  • STL:map用法总结

    一:介绍

    map是STL的关联式容器,以key-value的形式存储,以红黑树(平衡二叉查找树)作为底层数据结构,对数据有自动排序的功能。
    命名空间为std,所属头文件<map>

    二:常用操作

    容量:
    a.map中实际数据的数据:map.size()
    b.map中最大数据的数量:map.max_size()
    c.判断容器是否为空:map.empty()

    修改:
    a.插入数据:map.insert()
    b.清空map元素:map.clear()
    c.删除指定元素:map.erase(it)

    迭代器:
    a.map开始指针:map.begin()
    b.map尾部指针:map.end() 注:最后一个元素的下一个位置,类似为NULL,不是容器的最后一个元素

    三:存储

     1     map<int, string> map1;
     2 
     3     //方法1:
     4     map1.insert(pair<int, string>(2, "beijing"));
     5     //方法2:
     6     map1[4] = "changping";
     7     //方法3:
     8     map1.insert(map<int, string>::value_type(1, "huilongguan"));
     9     //方法4:
    10     map1.insert(make_pair<int, string>(3, "xierqi"));

    四:遍历

    1     for (map<int, string>::iterator it=map1.begin(); it!=map1.end(); it++)
    2     {
    3         cout << it->first << ":" << it->second << endl;
    4     }

    五:查找

     1     //方法1
     2     string value1 = map1[2];
     3     if (value1.empty())
     4     {
     5         cout << "not found" << endl;
     6     }
     7 
     8     //方法2
     9     map<int, string>::iterator it = map1.find(2);
    10     if (it == map1.end())
    11     {
    12         cout << "not found" << endl;
    13     }
    14     else
    15     {
    16         cout << it->first << ":" << it->second << endl;
    17     }

    六:修改

    map1[2] = "tianjin";

    七:删除

    1     //方法1
    2     map1.erase(1);
    3 
    4     //方法2
    5     map<int, string>::iterator it1 = map1.find(2);
    6     map1.erase(it1);

    扫码关注公众号

    专注分享Java,C/C++,STL,Spring框架,mybatis框架,mysql,redis,分布式,高并发,设计模式,爬虫,docker,shell编程等相关技术,在这里一起探讨,一起学习,一起进步,不定期分享视频书籍资源,充分利用碎片化时间,让我们的技术之路更加有乐趣。

  • 相关阅读:
    ORA-01935: missing user or role name
    ORA-00923: FROM keyword not found where expected
    ORA-00933: SQL command not properly ended
    ORA_12514:TNS:listener does not currently know of service requested in connect descriptor
    ORA-00918: column ambiguously defined
    ORA-01789: 查询块具有不正确的结果列数
    ORA-01789: query block has incorrect number of result columns
    ORA-01747
    ORA-01843: not a valid month
    ORA-00904: "I_LEVEL": invalid identifier
  • 原文地址:https://www.cnblogs.com/woniu201/p/9876955.html
Copyright © 2011-2022 走看看