STL_map使用方法.docVIP

  1. 1、本文档共9页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
  5. 5、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
  6. 6、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们
  7. 7、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
  8. 8、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
STL_map使用方法

STL中map用法详解 说明:如果你具备一定的C++ template知识,即使你没有接触过STL,这个文章你也应该可能较轻易的看懂。本人水平有限,不当之处,望大家辅正。 一.Map概述 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。 下面举例说明什么是一对一的数据映射。比如一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可能轻易描述,很明显学号用int描述,姓名用字符串描述(本篇文章中不用char *来描述字符串,而是采用STL中string来描述),下面给出map描述代码: Mapint, string mapStudent; map的构造函数 map共提供了6个构造函数,这块涉及到内存分配器这些东西,略过不表,在下面我们将接触到一些map的构造方法,这里要说下的就是,我们通常用如下方法构造一个map: Mapint, string mapStudent; 数据的插入 在构造map容器后,我们就可以往里面插入数据了。这里讲三种插入数据的方法: 第一种:用insert函数插入pair数据,下面举例说明(以下代码虽然是随手写的,应该可以在VC和GCC下编译通过,大家可以运行下看什么效果,在VC下请加入这条语句,屏蔽4786警告? #pragma warning (disable:4786) ) #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”)); ?mapint, string::iterator? iter; ?for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++) { ?Coutiter-first”?? ”iter-secondend; } } 第二种:用insert函数插入value_type数据,下面举例说明 #include map #include string #include iostream Using 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 iostream Using 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-f

文档评论(0)

aicencen + 关注
实名认证
文档贡献者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档