公司网站实用性,wordpress文章自动分页,wordpress主题图片路径设置,可视化编辑器wordpress一#xff0e;Map概述 Map是STL的一个关联容器#xff0c;它提供一对一#xff08;其中第一个可以称为关键字#xff0c;每个关键字只能在map中出现一次#xff0c;第二个可能称为该关键字的值#xff09;的数据处理能力#xff0c;由于这个特性#xff0c;它…一Map概述 Map是STL的一个关联容器它提供一对一其中第一个可以称为关键字每个关键字只能在map中出现一次第二个可能称为该关键字的值的数据处理能力由于这个特性它完成有可能在我们处理一对一数据的时候在编程上提供快速通道。这里说下map内部数据的组织map内部自建一颗红黑树(一种非严格意义上的平衡二叉树)这颗树具有对数据自动排序的功能所以在map内部所有的数据都是有序的后边我们会见识到有序的好处。 下面举例说明什么是一对一的数据映射。比如一个班级中每个学生的学号跟他的姓名就存在着一一映射的关系这个模型用map可能轻易描述很明显学号用int描述姓名用字符串描述(本篇文章中不用char *来描述字符串而是采用STL中string来描述),下面给出map描述代码 Mapint, string mapStudent; 二. 数据的插入 在构造map容器后我们就可以往里面插入数据了。这里讲三种插入数据的方法 第一种用insert函数插入pair数据下面举例说明(以下代码虽然是随手写的应该可以在VC和GCC下编译通过大家可以运行下看什么效果在VC下请加入这条语句屏蔽4786警告 pragma warning (disable:4786) )#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(mapint, string::value_type (1, student_one)); If(Insert_Pair.second true) { Cout”Insert Successfully”endl; } Else { Cout”Insert Failure”endl; } Insert_Pair mapStudent.insert(mapint, string::value_type (1, student_one)); 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; }} 三. map的构造函数 map共提供了6个构造函数这块涉及到内存分配器这些东西略过不表在下面我们将接触到一些map的构造方法 这里要说下的就是我们通常用如下方法构造一个map Mapint, string mapStudent; 四. map的大小 在往map里面插入了数据我们怎么知道当前已经插入了多少数据呢可以用size函数用法如下 Int nSize mapStudent.size(); 五. 数据的遍历 这里也提供三种方法对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 0; nIndex nSize; nIndex) { CoutmapStudent[nIndex]end; }}六. 数据的查找包括判定这个关键字是否在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; }}七. 数据的清空与判空 清空map中的数据可以用clear()函数判定map中是否有数据可以用empty()函数它返回true则说明是空map 八. 数据的删除 这里要用到erase函数它有三个重载了的函数下面在例子中详细说明它们的用法 #include map #include string #include iostream Using 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的特性删除区间是一个前闭后开的集合 //自个加上遍历代码打印输出吧 } 九. 其他一些函数用法 这里有swap,key_comp,value_comp,get_allocator等函数感觉到这些函数在编程用的不是很多略过不表有兴趣 的话可以自个研究 十. 排序 这里要讲的是一点比较高深的用法了,排序问题STL中默认是采用小于号来排序的以上代码在排序上是不存在任何问题 的因为上面的关键字是int型它本身支持小于号运算在一些特殊情况比如关键字是一个结构体涉及到排序就会出现问 题因为它没有小于号操作insert等函数在编译的时候过不去下面给出两个方法解决这个问题 第一种小于号重载程序举例 #include map #include string Using namespace std; Typedef struct tagStudentInfo { Int nID; String strName; }StudentInfo, *PStudentInfo; //学生信息 Int main() { //用学生信息映射分数 MapStudentInfo, intmapStudent; 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)); } 以上程序是无法编译通过的只要重载小于号就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 string Using 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)); } 转载于:https://www.cnblogs.com/ForEverKissing/archive/2008/05/06/1184705.html