• std::Map


    Map是STL的一个关联容器,它提供一对一的数据处理能力.
    map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。
    map的构造函数:供了6个构造函数

    数据插入

    1、mapStudent.insert(pair<int, string>(1, “student_one”));
    2、mapStudent.insert(map<int, string>::value_type (1, “student_one”));
    3、Map<int, string> mapStudent; mapStudent[1] =  “student_one”;
    
    当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念。
    1、insert操作是插入数据不了的,
    2、用数组方式,它可以覆盖以前该关键字对应的值。
    insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下 Pair
    <map<int, string>::iterator, bool> Insert_Pair; Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”)); 我们通过pair的第二个变量来知道是否插入成功.

    数据的遍历

    这里也提供三种方法,对map进行遍历
    第一种:应用前向迭代器,for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
    第二种:应用反相迭代器,for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
    第三种:用数组方式 int nSize = mapStudent.size();for(int nIndex = 1; nIndex <= nSize; nIndex++)

    数据的查找(包括判定这个关键字是否在map中出现)

    第一种:用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置, 由于map的特性,一对一的映射关系,就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1了
    第二种:用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器.
    第三种:这个方法用来判定数据是否出现,
       Lower_bound函数用法,这个函数用来返回要查找关键字的下界(是一个迭代器)
       Upper_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器)

    数据的清空与判空

    清空 clear()
    是否为空 empty()
    删除 
      it=m.find('b');
      m.erase (it);                   // erasing by iterator
      m.erase ('c');                  // erasing by key
      it=m.find ('e');
      m.erase ( it, m.end() );    // erasing by range//左闭右开

    排序

    排序问题,STL中默认是采用小于号来排序的,
    因为关键字是int型,它本身支持小于号运算,在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,
    因为它没有小于号操作,insert等函数在编译的时候过不去,下面给出两个方法解决这个问题
    #include <map>
    #include <string>
    Using namespace std;
    Typedef struct tagStudentInfo
    {
           Int      nID;
           String   strName;
           Bool operator < (tagStudentInfo const& _A) const
           {
                  //这个函数指定排序策略,按nID排序,如果nID相等的话,按strName排序
                  If(nID < _A.nID)  return true;
                  If(nID == _A.nID) return strName.compare(_A.strName) < 0;
                  Return false;
           }
    }StudentInfo, *PStudentInfo;  //学生信息
    Int main()
    {
      int nSize;
     //用学生信息映射分数
     map<StudentInfo, int>mapStudent;
     map<StudentInfo, int>::iterator iter;
     
     StudentInfo studentInfo;
     studentInfo.nID = 1;
     studentInfo.strName = “student_one”;
     mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));
     
     studentInfo.nID = 2;
     studentInfo.strName = “student_two”;
     mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));
    }
  • 相关阅读:
    学习类的网站链接
    聊聊Web App、Hybrid App与Native App的设计差异
    Telnet是什么意思又是什么协议 Telnet有什么作用及功能
    telnet命令的使用方法
    HTTP协议详解(真的很经典)(转载)
    数据导入报错:Got a packet bigger than‘max_allowed_packet’bytes的问题
    Mysql mysql lost connection to server during query 问题解决方法
    MySQL 分组之后如何统计记录条数 gourp by 之后的 count()
    学习生活必须知道的网站或者App
    shell脚本使用## or %%
  • 原文地址:https://www.cnblogs.com/osbreak/p/9206591.html
Copyright © 2020-2023  润新知