建设工程消防验收查询网站,公司名字变了网站备案,网络公司 网站建设 小程序,山西建设执业资格注册管理中心网站cMap是STL的一个关联容器#xff0c;它提供一对一#xff08;其中第一个可以称为关键字#xff0c;每个关键字只能在map中出现一次#xff0c;第二个可能称为该关键字的值#xff09;的数据处理能力#xff0c;由于这个特性#xff0c;它完成有可能在我们处理一对一数据…cMap是STL的一个关联容器它提供一对一其中第一个可以称为关键字每个关键字只能在map中出现一次第二个可能称为该关键字的值的数据处理能力由于这个特性它完成有可能在我们处理一对一数据的时候在编程上提供快速通道。这里说下map内部数据的组织map内部自建一颗红黑树(一种非严格意义上的平衡二叉树)这颗树具有对数据自动排序的功能所以在map内部所有的数据都是有序的后边我们会见识到有序的好处。1. map的构造函数mapint, string maphai;mapchar,int maphai;mapstring,char mapstring;mapstring,int mapstring;mapint ,charmapint;mapchar,stringmapchar;2. 数据的插入在构造map容器后我们就可以往里面插入数据了。这里讲三种插入数据的方法第一种用insert函数插入pair数据#include map#include string#include iostreamUsing namespace std;Int main(){Mapint, string mapStudent;mapStudent.insert(pairint, string(1, “student_one”));mapStudent.insert(pairint, string(2, “student_two”));mapStudent.insert(pairint, string(3, “student_three”));mapint, string::iterator iter;for(iter mapStudent.begin(); iter ! mapStudent.end(); iter){Coutiter-first” ”iter-secondend;}}第二种用insert函数插入value_type数据下面举例说明#include map#include string#include iostreamUsing namespace std;Int main(){Mapint, string mapStudent;mapStudent.insert(mapint, string::value_type (1, “student_one”));mapStudent.insert(mapint, string::value_type (2, “student_two”));mapStudent.insert(mapint, string::value_type (3, “student_three”));mapint, string::iterator iter;for(iter mapStudent.begin(); iter ! mapStudent.end(); iter){Coutiter-first” ”iter-secondend;}}第三种用数组方式插入数据下面举例说明#include map#include string#include iostreamUsing namespace std;Int main(){Mapint, string mapStudent;mapStudent[1] “student_one”;mapStudent[2] “student_two”;mapStudent[3] “student_three”;mapint, string::iterator iter;for(iter mapStudent.begin(); iter ! mapStudent.end(); iter){Coutiter-first” ”iter-secondend;}}以上三种用法虽然都可以实现数据的插入但是它们是有区别的当然了第一种和第二种在效果上是完成一样的用insert函数插入数据在数据的插入上涉及到集合的唯一性这个概念即当map中有这个关键字时insert操作是插入数据不了的但是用数组方式就不同了它可以覆盖以前该关键字对应的值用程序说明mapStudent.insert(mapint, string::value_type (1, “student_one”));mapStudent.insert(mapint, string::value_type (1, “student_two”));上面这两条语句执行后map中1这个关键字对应的值是“student_one”第二条语句并没有生效那么这就涉及到我们怎么知道insert语句是否插入成功的问题了可以用pair来获得是否插入成功程序如下Pairmapint, string::iterator, bool Insert_Pair;Insert_Pair mapStudent.insert(mapint, string::value_type (1, “student_one”));我们通过pair的第二个变量来知道是否插入成功它的第一个变量返回的是一个map的迭代器如果插入成功的话Insert_Pair.second应该是true的否则为false。下面给出完成代码演示插入成功与否问题#include map#include string#include iostreamUsing namespace std;Int main(){Mapint, string mapStudent;Pairmapint, string::iterator, bool Insert_Pair;Insert_Pair mapStudent.insert(pairint, string(1, “student_one”));If(Insert_Pair.second true){Cout”Insert Successfully”endl;}Else{Cout”Insert Failure”endl;}Insert_Pair mapStudent.insert(pairint, string(1, “student_two”));If(Insert_Pair.second true){Cout”Insert Successfully”endl;}Else{Cout”Insert Failure”endl;}mapint, string::iterator iter;for(iter mapStudent.begin(); iter ! mapStudent.end(); iter){Coutiter-first” ”iter-secondend;}}大家可以用如下程序看下用数组插入在数据覆盖上的效果#include map#include string#include iostreamUsing namespace std;Int main(){Mapint, string mapStudent;mapStudent[1] “student_one”;mapStudent[1] “student_two”;mapStudent[2] “student_three”;mapint, string::iterator iter;for(iter mapStudent.begin(); iter ! mapStudent.end(); iter){Coutiter-first” ”iter-secondend;}}3. map的大小在往map里面插入了数据我们怎么知道当前已经插入了多少数据呢可以用size函数用法如下Int nSize mapStudent.size();4. 数据的遍历这里也提供三种方法对map进行遍历第一种应用前向迭代器上面举例程序中到处都是了略过不表第二种应用反相迭代器下面举例说明要体会效果请自个动手运行程序#include map#include string#include iostreamUsing namespace std;Int main(){Mapint, string mapStudent;mapStudent.insert(pairint, string(1, “student_one”));mapStudent.insert(pairint, string(2, “student_two”));mapStudent.insert(pairint, string(3, “student_three”));mapint, string::reverse_iterator iter;for(iter mapStudent.rbegin(); iter ! mapStudent.rend(); iter){Coutiter-first” ”iter-secondend;}}第三种用数组方式程序说明如下#include map#include string#include iostreamUsing namespace std;Int main(){Mapint, string mapStudent;mapStudent.insert(pairint, string(1, “student_one”));mapStudent.insert(pairint, string(2, “student_two”));mapStudent.insert(pairint, string(3, “student_three”));int nSize mapStudent.size()//此处有误应该是 for(int nIndex 1; nIndex nSize; nIndex) //by rainfishfor(int nIndex 0; nIndex nSize; nIndex){CoutmapStudent[nIndex]end;}}5. 数据的查找包括判定这个关键字是否在map中出现在这里我们将体会map在数据插入时保证有序的好处。要判定一个数据关键字是否在map中出现的方法比较多这里标题虽然是数据的查找在这里将穿插着大量的map基本用法。这里给出三种数据查找方法第一种用count函数来判定关键字是否出现其缺点是无法定位数据出现位置,由于map的特性一对一的映射关系就决定了count函数的返回值只有两个要么是0要么是1出现的情况当然是返回1了第二种用find函数来定位数据出现位置它返回的一个迭代器当数据出现时它返回数据所在位置的迭代器如果map中没有要查找的数据它返回的迭代器等于end函数返回的迭代器程序说明#include map#include string#include iostreamUsing namespace std;Int main(){Mapint, string mapStudent;mapStudent.insert(pairint, string(1, “student_one”));mapStudent.insert(pairint, string(2, “student_two”));mapStudent.insert(pairint, string(3, “student_three”));mapint, string::iterator iter;iter mapStudent.find(1);if(iter ! mapStudent.end()){Cout”Find, the value is ”iter-secondendl;}Else{Cout”Do not Find”endl;}}第三种这个方法用来判定数据是否出现是显得笨了点但是我打算在这里讲解Lower_bound函数用法这个函数用来返回要查找关键字的下界(是一个迭代器)Upper_bound函数用法这个函数用来返回要查找关键字的上界(是一个迭代器)例如map中已经插入了1234的话如果lower_bound(2)的话返回的2而upper-bound2的话返回的就是3Equal_range函数返回一个pairpair里面第一个变量是Lower_bound返回的迭代器pair里面第二个迭代器是Upper_bound返回的迭代器如果这两个迭代器相等的话则说明map中不出现这个关键字程序说明#include map#include string#include iostreamUsing namespace std;Int main(){Mapint, string mapStudent;mapStudent[1] “student_one”;mapStudent[3] “student_three”;mapStudent[5] “student_five”;mapint, string::iterator iter;iter mapStudent.lower_bound(2);{//返回的是下界3的迭代器Coutiter-secondendl;}iter mapStudent.lower_bound(3);{//返回的是下界3的迭代器Coutiter-secondendl;}iter mapStudent.upper_bound(2);{//返回的是上界3的迭代器Coutiter-secondendl;}iter mapStudent.upper_bound(3);{//返回的是上界5的迭代器Coutiter-secondendl;}Pairmapint, string::iterator, mapint, string::iterator mapPair;mapPair mapStudent.equal_range(2);if(mapPair.first mapPair.second){cout”Do not Find”endl;}Else{Cout”Find”endl;}mapPair mapStudent.equal_range(3);if(mapPair.first mapPair.second){cout”Do not Find”endl;}Else{Cout”Find”endl;}}6. 数据的清空与判空清空map中的数据可以用clear()函数判定map中是否有数据可以用empty()函数它返回true则说明是空map7. 数据的删除这里要用到erase函数它有三个重载了的函数下面在例子中详细说明它们的用法#include map#include string#include iostreamUsing namespace std;Int main(){Mapint, string mapStudent;mapStudent.insert(pairint, string(1, “student_one”));mapStudent.insert(pairint, string(2, “student_two”));mapStudent.insert(pairint, string(3, “student_three”));//如果你要演示输出效果请选择以下的一种你看到的效果会比较好//如果要删除1,用迭代器删除mapint, string::iterator iter;iter mapStudent.find(1);mapStudent.erase(iter);//如果要删除1用关键字删除Int n mapStudent.erase(1);//如果删除了会返回1否则返回0//用迭代器成片的删除//一下代码把整个map清空mapStudent.earse(mapStudent.begin(), mapStudent.end());//成片删除要注意的是也是STL的特性删除区间是一个前闭后开的集合//自个加上遍历代码打印输出吧}8. 其他一些函数用法这里有swap,key_comp,value_comp,get_allocator等函数感觉到这些函数在编程用的不是很多略过不表有兴趣的话可以自个研究9. 排序这里要讲的是一点比较高深的用法了,排序问题STL中默认是采用小于号来排序的以上代码在排序上是不存在任何问题的因为上面的关键字是int型它本身支持小于号运算在一些特殊情况比如关键字是一个结构体涉及到排序就会出现问题因为它没有小于号操作insert等函数在编译的时候过不去下面给出两个方法解决这个问题第一种小于号重载程序举例#include map#include stringUsing namespace std;Typedef struct tagStudentInfo{Int nID;String strName;}StudentInfo, *PStudentInfo; //学生信息Int main(){int nSize;//用学生信息映射分数mapStudentInfo, intmapStudent;mapStudentInfo, int::iterator iter;StudentInfo studentInfo;studentInfo.nID 1;studentInfo.strName “student_one”;mapStudent.insert(pairStudentInfo, int(studentInfo, 90));studentInfo.nID 2;studentInfo.strName “student_two”;mapStudent.insert(pairStudentInfo, int(studentInfo, 80));for (itermapStudent.begin(); iter!mapStudent.end(); iter)coutiter-first.nIDendliter-first.strNameendliter-secondendl;}以上程序是无法编译通过的只要重载小于号就OK了如下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; //学生信息第二种仿函数的应用这个时候结构体中没有直接的小于号重载程序说明#include map#include stringUsing namespace std;Typedef struct tagStudentInfo{Int nID;String strName;}StudentInfo, *PStudentInfo; //学生信息Classs sort{Public:Bool operator() (StudentInfo const _A, StudentInfo const _B) const{If(_A.nID _B.nID) return true;If(_A.nID _B.nID) return _A.strName.compare(_B.strName) 0;Return false;}};Int main(){//用学生信息映射分数MapStudentInfo, int, sortmapStudent;StudentInfo studentInfo;studentInfo.nID 1;studentInfo.strName “student_one”;mapStudent.insert(pairStudentInfo, int(studentInfo, 90));studentInfo.nID 2;studentInfo.strName “student_two”;mapStudent.insert(pairStudentInfo, int(studentInfo, 80));}