C++ Map基本操作

来源:互联网 发布:黎明杀机淘宝好便宜 编辑:程序博客网 时间:2024/05/24 05:34
       std map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下std map内部数据的组织,std map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在std map内部所有的数据都是有序的,后边我们会见识到有序的好处。

      下面举例说明什么是一对一的数据映射。比如一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可能轻易描述,很明显学号用int描述,姓名用字符串描述(本篇文章中不用char *来描述字符串,而是采用STL中string来描述),下面给出map描述代码:

#pragma warning (disable:4786)#include <map>#include <string>#include <iostream>using namespace std;void print(map<int,string> &maps){map<int,string>::iterator iter;cout<<endl;for(iter=maps.begin(); iter!=maps.end(); iter++){cout<<iter->first<<" "<<iter->second<<endl;}cout<<endl;}int main(){map<int,string> mapStudent;mapStudent[1]="student_zero";                                         ///1mapStudent[2]="student_two";mapStudent[1]="student_one";mapStudent.insert(pair<int,string>(3,"student_three"));              ///2mapStudent.insert(pair<int,string>(4,"student_four"));mapStudent.insert(map<int,string>::value_type(5,"student_five"));    ///3mapStudent.insert(map<int,string>::value_type(6,"student_six"));    print(mapStudent);pair<map<int,string>::iterator,bool> insert_pair;insert_pair=mapStudent.insert(pair<int,string>(6,"student_six_2"));if(insert_pair.second==true)cout<<"insert 6 successfully"<<endl;elsecout<<"insert 6 failed"<<endl;insert_pair=mapStudent.insert(pair<int,string>(7,"student_seven"));if(insert_pair.second==true)cout<<"insert 7 successfully"<<endl;elsecout<<"insert 7 failed"<<endl;print(mapStudent);////数据的查找map<int,string>::iterator it;it=mapStudent.find(3);if(it!=mapStudent.end())cout<<"find,the value is "<<it->second<<endl;///删除元素it=mapStudent.find(3);if(it!=mapStudent.end()){it=mapStudent.erase(it);   ///返回删除元素的下一个节点if(it!=mapStudent.end()){cout<<it->first<<" "<<it->second<<endl;}}int ret=mapStudent.erase(3);cout<<"ret: "<<ret<<endl;ret=mapStudent.erase(4);cout<<"ret: "<<ret<<endl;print(mapStudent);mapStudent.erase(mapStudent.begin(),mapStudent.end());return 0;}

输出为:

1 student_one
2 student_two
3 student_three
4 student_four
5 student_five
6 student_six

insert 6 failed
insert 7 successfully

1 student_one
2 student_two
3 student_three
4 student_four
5 student_five
6 student_six
7 student_seven

find,the value is student_three
4 student_four
ret: 0
ret: 1

1 student_one
2 student_two
5 student_five
6 student_six
7 student_seven


      以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明

mapStudent.insert(map<int, string>::value_type (1, “student_one”));
mapStudent.insert(map<int, string>::value_type (1, “student_two”));

      上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下

Pair<map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));

      我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。


转自:http://www.yitsoft.com/chap_study/ch_00029/ch_00029.asp