STL中map用法详解

上传人:油条 文档编号:12418655 上传时间:2017-09-03 格式:PDF 页数:15 大小:178.69KB
返回 下载 相关 举报
STL中map用法详解_第1页
第1页 / 共15页
STL中map用法详解_第2页
第2页 / 共15页
STL中map用法详解_第3页
第3页 / 共15页
STL中map用法详解_第4页
第4页 / 共15页
STL中map用法详解_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《STL中map用法详解》由会员分享,可在线阅读,更多相关《STL中map用法详解(15页珍藏版)》请在金锄头文库上搜索。

1、Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。下面举例说明什么是一对一的数据映射。比如一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可能轻易描述,很明显学号用int描述,姓名用字符串描述(本篇文章中用char

2、*来描述字符串,而是采用STL中string来描述),下面给出map描述代码:MapmapStudent1.map的构造函数map共提供6个构造函数,这块涉及到内存分配器这些东西,略过表,在下面我们将接触到一些map的构造方法,这里要说下的就是,我们通常用如下方法构造一个map:MapmapStudent2.数据的插入在构造map容器后,我们就可以往里面插入数据。这里讲三种插入数据的方法:第一种:用insert函数插入pair数据,下面举例说明(以下代码虽然是随手写的,应该可以在VC和GCC下编译通过,大家可以运行下看什么效果,在VC下请加入这条语句,屏蔽4786警告pragmawarning

3、(disable:4786)#include#include#includeUsingnamespacestdIntmain()MapmapStudentmapStudent.insert(pair(1,“student_one”)mapStudent.insert(pair(2,“student_two”)mapStudent.insert(pair(3,“student_three”)map:iteratoriterfor(iter=mapStudent.begin()iter!=mapStudent.end()iter+)Coutfirstsecond#include#includeUs

4、ingnamespacestdIntmain()MapmapStudentmapStudent.insert(map:value_type(1,“student_one”)mapStudent.insert(map:value_type(2,“student_two”)mapStudent.insert(map:value_type(3,“student_three”)map:iteratoriterfor(iter=mapStudent.begin()iter!=mapStudent.end()iter+)Coutfirstsecond#include#includeUsingnamespa

5、cestdIntmain()MapmapStudentmapStudent1=“student_one”mapStudent2=“student_two”mapStudent3=“student_three”map:iteratoriterfor(iter=mapStudent.begin()iter!=mapStudent.end()iter+)Coutfirstsecond:value_type(1,“student_one”)mapStudent.insert(map:value_type(1,“student_two”)上面这两条语句执行后,map中1这个关键字对应的值是“studen

6、t_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题,可以用pair来获得是否插入成功,程序如下Pair:iterator,boolInsert_PairInsert_Pair=mapStudent.insert(map:value_type(1,“student_one”)我们通过pair的第二个变来知道是否插入成功,它的第一个变返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。下面给出完成代码,演示插入成功与否问题#include#include#includeUsingnames

7、pacestdIntmain()MapmapStudentPair:iterator,boolInsert_PairInsert_PairmapStudent.insert(pair(1,“student_one”)If(Insert_Pair.second=true)Cout(1,“student_two”)If(Insert_Pair.second=true)Cout:iteratoriterfor(iter=mapStudent.begin()iter!=mapStudent.end()iter+)Coutfirstsecond#include#includeUsingnamespace

8、stdIntmain()MapmapStudentmapStudent1=“student_one”mapStudent1=“student_two”mapStudent2=“student_three”map:iteratoriterfor(iter=mapStudent.begin()iter!=mapStudent.end()iter+)Coutfirstsecond#include#includeUsingnamespacestdIntmain()MapmapStudentmapStudent.insert(pair(1,“student_one”)mapStudent.insert(

9、pair(2,“student_two”)mapStudent.insert(pair(3,“student_three”)map:reverse_iteratoriterfor(iter=mapStudent.rbegin()iter!=mapStudent.rend()iter+)Coutfirstsecond#include#includeUsingnamespacestdIntmain()MapmapStudentmapStudent.insert(pair(1,“student_one”)mapStudent.insert(pair(2,“student_two”)mapStuden

10、t.insert(pair(3,“student_three”)intnSize=mapStudent.size()/此处有误,应该是for(intnIndex=1nIndex#include#includeUsingnamespacestdIntmain()MapmapStudentmapStudent.insert(pair(1,“student_one”)mapStudent.insert(pair(2,“student_two”)mapStudent.insert(pair(3,“student_three”)map:iteratoriteriter=mapStudent.find(1

11、)if(iter!=mapStudent.end()Coutsecond#include#includeUsingnamespacestdIntmain()MapmapStudentmapStudent1=“student_one”mapStudent3=“student_three”mapStudent5=“student_five”map:iteratoriteriter=mapStudent.lower_bound(2)/返回的是下界3的迭代器Coutsecondsecondsecondsecond:iterator,map:iteratormapPairmapPair=mapStude

12、nt.equal_range(2)if(mapPair.first=mapPair.second)cout#include#includeUsingnamespacestdIntmain()MapmapStudentmapStudent.insert(pair(1,“student_one”)mapStudent.insert(pair(2,“student_two”)mapStudent.insert(pair(3,“student_three”)/如果你要演示输出效果,请选择以下的一种,你看到的效果会比较好/如果要删除1,用迭代器删除map:iteratoriteriter=mapStud

13、ent.find(1)mapStudent.erase(iter)/如果要删除1,用关键字删除Intn=mapStudent.erase(1)/如果删除会返回1,否则返回0/用迭代器,成片的删除/一下代码把整个map清空mapStudent.earse(mapStudent.begin(),mapStudent.end()/成片删除要注意的是,也是STL的特性,删除区间是一个前闭后开的集合/自个加上遍历代码,打印输出吧8.其他一些函数用法这里有swap,key_comp,value_comp,get_allocator等函数,感觉到这些函数在编程用的是很多,略过表,有兴趣的话可以自个研究9.排

14、序这里要讲的是一点比较高深的用法,排序问题,STL中默认是采用小于号来排序的,以上代码在排序上是存在任何问题的,因为上面的关键字是int型,它本身支持小于号运算,在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,因为它没有小于号操作,insert等函数在编译的时候过去,下面给出两个方法解决这个问题第一种:小于号重载,程序举例#include#includeUsingnamespacestdTypedefstructtagStudentInfoIntnIDStringstrNameStudentInfo,*PStudentInfo/学生信息Intmain()intnSize/用学生信息映射分数mapmapStudentmap:iteratoriterStudentInfostudentInfostudentInfo.nID=1studentInfo.strName=“student_one”mapStudent.insert(pair(studentInfo,90)studentInfo.nID=2studentInfo.strName=“student_two”mapStudent.insert(pair(studentInfo,80)for(iter=mapStudent.b

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号