首页 STL-Map篇

STL-Map篇

举报
开通vip

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

STL-Map篇
一、Map篇 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在处理一对一数据时,在编程上提供快速通道。介绍一下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后面会见识到有序的好处。 下面举例说明什么是一对一的数据映射。比如一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可能轻易描述,很明显学号用int描述,姓名用字符串描述(本篇文章中不用char *来描述字符串,而是采用STL中string来描述),下面给出map描述代码: 1.map的构造函数 map共提供了6个构造函数,在下面将接触到一些map的构造方法,我们通常用如下方法构造一个map: map mapStudent; 2.数据的插入 在构造map容器后,就可以往里面插入数据了。这里介绍三种插入数据的方法: 第一种:用insert函数插入pair数据(直接在后面插就可以) #include #include #include using namespace std; int main() { map mapStudent; mapStudent.insert(pair(1,"student_one")); mapStudent.insert(pair(2,"student_two")); mapStudent.insert(pair(3,"student_three")); map::iterator iter; for(iter=mapStudent.begin();iter!= mapStudent.end();iter++) cout<<(*iter).first<<" "<second<first可以(*iter).first } 第二种:用insert函数插入value_type数据 #include #include #include using namespace std; int main() { map mapStudent; mapStudent.insert(map::value_type(1,"student_one")); mapStudent.insert(map::value_type(2,"student_two")); mapStudent.insert(map::value_type(3,"student_three")); map::iterator iter; for(iter=mapStudent.begin();iter!= mapStudent.end();iter++) cout<first<<" "<second< #include #include using namespace std; int main() { map mapStudent; mapStudent[1]="student_one"; mapStudent[2]="student_two"; mapStudent[3]="student_three"; map::iterator iter; for(iter=mapStudent.begin();iter!= mapStudent.end();iter++) cout<first<<" "<second<::value_type(1,"student_one")); mapStudent.insert(map::value_type(1,"student_one")); 上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下: pair::iterator, bool> insert_pair; insert_pair=mapStudent.insert(map::value_type(1,"student_one")); 我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。 下面给出完成代码,演示插入成功与否问题 #include #include #include using namespace std; int main() { map mapStudent; pair::iterator,bool> insert_pair; insert_pair=mapStudent.insert(pair(1,"student_one")); if(insert_pair.second==true) cout<<"insert Successfully"<(1,"student_two")); if(insert_pair.second==true) cout<<"Insert Successfully"<::iterator iter; for(iter=mapStudent.begin();iter!= mapStudent.end();iter++) cout<first<<" "<second< #include #include using namespace std; int main() { map mapStudent; mapStudent[1]="student_one"; mapStudent[1]="student_two"; mapStudent[2]="student_three"; map::iterator iter; for(iter=mapStudent.begin();iter!= mapStudent.end();iter++) cout<first<<" "<second< #include #include using namespace std; int main() { map mapStudent; mapStudent[1]="student_one"; mapStudent[1]="student_two"; mapStudent[3]="student_three"; map::iterator iter; for(iter=mapStudent.begin();iter!= mapStudent.end();iter++) cout<first<<" "<second< #include #include using namespace std; int main() { map mapStudent; mapStudent.insert(pair(1,"student_one")); mapStudent.insert(pair(2,"student_two")); mapStudent.insert(pair(3,"student_three")); map::reverse_iterator iter; for(iter=mapStudent.rbegin();iter!= mapStudent.rend();iter++) cout<first<<" "<second< #include #include using namespace std; int main() { map mapStudent; mapStudent.insert(pair(1,"student_one")); mapStudent.insert(pair(2,"student_two")); mapStudent.insert(pair(3,"student_three")); int nSize=mapStudent.size(); for(int i=0;i<=nSize;i++) cout< #include #include using namespace std; int main() { map mapStudent; mapStudent.insert(pair(1,"student_one")); mapStudent.insert(pair(2,"student_two")); mapStudent.insert(pair(3,"student_three")); map::reverse_iterator iter; int t1,t2; t1=mapStudent.count(4); t2=mapStudent.count(1); cout< #include #include using namespace std; int main() { map mapStudent; mapStudent.insert(pair(1,"student_one")); mapStudent.insert(pair(2,"student_two")); mapStudent.insert(pair(3,"student_three")); map::iterator iter; iter=mapStudent.find(1); if(iter!=mapStudent.end()) cout<<"Find, the value is: "<second< #include #include using namespace std; int main() { map mapStudent; mapStudent.insert(pair(1,"student_one")); mapStudent.insert(pair(2,"student_two")); mapStudent.insert(pair(3,"student_three")); map::iterator iter; iter=mapStudent.lower_bound(2);//返回的是下界的迭代器 cout<second<second<second<::iterator, map::iterator> mapPair; mapPair=mapStudent.equal_range(2); if(mapPair.first==mapPair.second) cout<<"Do not Find"<first<<" "<second< #include #include using namespace std; int main() { map mapStudent; mapStudent.insert(pair(1,"student_one")); mapStudent.insert(pair(2,"student_two")); mapStudent.insert(pair(3,"student_three")); map::reverse_iterator iter; int t; mapStudent.clear(); t=mapStudent.empty(); cout< #include #include using namespace std; int main() { map mapStudent; mapStudent.insert(pair(1,"student_one")); mapStudent.insert(pair(2,"student_two")); mapStudent.insert(pair(3,"student_three")); //如果要演示输出效果,请选择以下的一种,看到的效果会比较好 //如果要删除,用迭代器删除 map::iterator iter; iter=mapStudent.find(1); mapStudent.erase(iter); //如果要删除,用关键字删除 int n=mapStudent.erase(1);//如果删除了n会返回,否则返回 //用迭代器,成片的删除 mapStudent.erase(mapStudent.begin(),mapStudent.end()); //一下代码把整个map清空 mapStudent.erase(mapStudent.begin(),mapStudent.end()); //成片删除要注意的是,也是STL的特性,删除区间是一个前闭后开的集合 //自个加上遍历代码,打印输出吧 return 0; } 8.其他一些函数用法 这里有swap,key_comp,value_comp,get_allocator等函数,这些函数在编程用的不是很多 9.排序 STL中默认是采用小于号来排序的,以上代码在排序上是不存在任何问题的,因为上面的关键字是int型,它本身支持小于号运算,在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,因为它没有小于号操作,insert等函数在编译的时候过不去,下面给出两个方法解决这个问题 第一种:小于号重载,程序举例: #include #include using namespace std; typedef struct tagStudentInfo { int nID; string strName; bool operator<(tagStudentInfo const& A) const { //这个函数指定排序策略,按nID排序,如果nID相等的话,按strName排序 if(nID mapStudent; StudentInfo studentInfo; studentInfo.nID=1; studentInfo.strName="student_one"; mapStudent.insert(pair(studentInfo,90)); studentInfo.nID=2; studentInfo.strName="student_two"; mapStudent.insert(pair(studentInfo,80)); } 10.另外 由于STL是一个统一的整体,map的很多用法都和STL中其它的东西结合在一起,比如在排序上,这里默认用的是小于号,即less<>,如果要从大到小排序呢,这里涉及到的东西很多,在此无法一一加以说明。 还要说明的是,map中由于它内部有序,由红黑树保证,因此很多函数执行的时间复杂度都是log2N的,如果用map函数可以实现的功能,而STL Algorithm也可以完成该功能,建议用map自带函数,效率高一些。
本文档为【STL-Map篇】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_172471
暂无简介~
格式:doc
大小:71KB
软件:Word
页数:9
分类:互联网
上传时间:2011-09-27
浏览量:20