- 1、本文档共9页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
STL中map用法详解
说明:如果你具备一定的C++ template知识,即使你没有接触过STL,这个文章你也应该可能较轻易的看懂。本人水平有限,不当之处,望大家辅正。
一.Map概述
Map是STL的一个关联容器,它提供一对一〔其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值〕的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下mapmap(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map
mapint描述,姓名用字符串描述(本篇文章中不用char *来描述字符串,而是采用STL中string来描述),下面给出map描述代码:
Mapint, string mapStudent;
1. map
map共提供了6mapmap:
Mapint, string mapStudent;
2. 数据的插入
在构造map
第一种:用insertpair数据,下面举例说明(以下代码虽然是随手写的,应该可以在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;
}
}
第二种:用insertvalue_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-first〞 〞iter-secondend;
}
}
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用ins
文档评论(0)