zoukankan      html  css  js  c++  java
  • c++ STL之map

    map内部自建一颗红黑树(一 种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,map中的元素是自动按Key升序排序,所以不能对map用sort函数;

    map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。

    对于迭代器来说,可以修改实值,而不能修改key。

     

    使用map得包含map类所在的头文件

    #include <map>  //注意,STL头文件没有扩展名.h

     

    插入数据:

      用insert函数插入pair数据

                  

    map<int, string> mapStudent;  
      
        mapStudent.insert(pair<int, string>(1, "student_one"));  

      用insert函数插入value_type数据

    map<int, string> mapStudent;  
      
        mapStudent.insert(map<int, string>::value_type (1, "student_one"));  

      用数组方式插入数据

    map<int, string> mapStudent;  
      
        mapStudent[1] = "student_one";  
      
        mapStudent[2] = "student_two";  
      
        mapStudent[3] = "student_three";  

    pair和value_type不覆盖已有的key值对,数组覆盖已有的key值对

    第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的 插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对 应的值,

    数据的遍历

      应用前向迭代器

    for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
      
           cout<<iter->first<<' '<<iter->second<<endl;  

      应用反相迭代器

    for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)  
      
            cout<<iter->first<<"  "<<iter->second<<endl; 

      用数组的形式

    int nSize = mapStudent.size();  
      
    //此处应注意,应该是 for(int nindex = 1; nindex <= nSize; nindex++)  
    //而不是 for(int nindex = 0; nindex < nSize; nindex++)  
      
        for(int nindex = 1; nindex <= nSize; nindex++)  
      
            cout<<mapStudent[nindex]<<endl;  

    两个数据 iterator->first和 iterator->second分别代表关键字和存储的数据。

    查找并获取map中的元素

      用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,由于map的特性,一对一的映射关系,就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1了

      用find函数来定位数据出现位置,传入的参数是要查找的key,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器。

    map的基本操作函数:

         C++ maps是一种关联式容器,包含“关键字/值”对

         begin()         返回指向map头部的迭代器

         clear()        删除所有元素

         count()         返回指定元素出现的次数

         empty()         如果map为空则返回true

         end()           返回指向map末尾的迭代器

         equal_range()   返回特殊条目的迭代器对

         erase()         删除一个元素

         find()          查找一个元素

         get_allocator() 返回map的配置器

         insert()        插入元素

         key_comp()      返回比较元素key的函数

         lower_bound()   返回键值>=给定元素的第一个位置

         max_size()      返回可以容纳的最大元素个数

         rbegin()        返回一个指向map尾部的逆向迭代器

         rend()          返回一个指向map头部的逆向迭代器

         size()          返回map中元素的个数

         swap()           交换两个map

         upper_bound()    返回键值>给定元素的第一个位置

         value_comp()     返回比较元素value的函数

          

    参考文章:

    https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html

    http://classfoo.com/ccby/article/WQ9qz

    https://blog.csdn.net/zhimeng567/article/details/78546199

  • 相关阅读:
    Unity—Json2
    Unity—Json1
    windows 命令行报错:file(s) not in client view
    winform listbox 使用DrawMode使用OwnerDrawVarialbe或OwnerDrawFixed无水平滚动条
    windows 命令行使用p4
    winform listbox增加鼠标双击事件
    lua5.3+luasocket
    centos 安装glide工具(golang)笔记
    centos 安装golang笔记
    centos安装redis,并设置开机自动启动项
  • 原文地址:https://www.cnblogs.com/xiaoxue126/p/8997764.html
Copyright © 2011-2022 走看看