STL—— map用法

还有一篇讲map的文章:

https://blog.csdn.net/shuzfan/article/details/53115922

 

应用map的题:http://codeforces.com/contest/1005/problem/C

map里可以有十几万个不同关键字!!!

  map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)

1.我们通常用如下方法构造一个map

[cpp] view plain copy

  1. map<int, string> mapStudent;  

2.数据的插入

有三种插入数据的方法 ①pair<int,string>( , ) 或make_pair( , ) ② insert函数插入value_type数据 ③ 数组法

[cpp] view plain copy

  1. #include <map>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.        map<int, string> mapStudent;
  8.        mapStudent.insert(pair<int, string>(1, "student_one"));  
  9.        mapStudent.insert(make_pair(2, "student_two"));  
  10.        mapStudent.insert(map<int, string>::value_type (3, "student_three"));  
  11.        mapStudent[4] =  "student_four";  
  12.  //前向迭代器iterator遍历
  13.        map<int, string>::iterator  iter;  
  14.        for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  15.           cout<<iter->first<<"  "<<iter->second<<endl;     
  16.  //反向迭代器reverse_iterator遍历
  17.        map<int, string>::reverse_iterator  iter;  
  18.        for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)    
  19.             cout<<iter->first<<"   "<<iter->second<<endl;
  20.   //数组遍历
  21. }  

以上三种用法第一种和第二种在效果上是完全一样的,用insert函数插入数据,当map中有这个关键字时,insert操作无效,但是用数组方式就可以覆盖以前该关键字对应的值,用程序说明

[cpp] view plain copy

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

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

[cpp] view plain copy

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

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

 

3.map的大小

在往map里面插入了数据,我们怎么知道当前已经插入了多少数据呢,可以用size函数,用法如下:

[cpp] view plain copy

  1. int nSize = mapStudent.size();  

4.数据的遍历

可以用迭代器,也可以用数组方式(貌似只适用于键值是数字比较特殊的)

用数组方式,程序说明如下

[cpp] view plain copy

  1. #include <map>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.        map<int, string> mapStudent;  
  8.        mapStudent.insert(pair<int, string>(1, "student_one"));  
  9.        mapStudent.insert(pair<int, string>(2, "student_two"));  
  10.        mapStudent.insert(pair<int, string>(3, "student_three"));  
  11.        int nSize = mapStudent.size()  
  12. //此处有误,应该是 for(int nIndex = 1; nIndex <= nSize; nIndex++)   
  13. //by rainfish  
  14.        for(int nIndex = 0; nIndex < nSize; nIndex++)  
  15.        {  
  16.            cout<<mapStudent[nIndex]<<end;  
  17.        }  
  18. }  

5.map的常用函数

 

 

查找(查找的是键)

使用count,返回的是被查找元素的个数。如果有,返回1;否则,返回0。注意,map中不存在相同元素,所以返回值只能是1或0。使用find,返回的是迭代器,如果找到了返回被查找元素的位置,没有则返回map.end()。

[cpp] view plain copy

  1. #include <map>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.        map<int, string> mapStudent;  
  8.        mapStudent.insert(pair<int, string>(1, "student_one"));  
  9.        mapStudent.insert(pair<int, string>(2, "student_two"));  
  10.        mapStudent.insert(pair<int, string>(3, "student_three"));  
  11.        map<int, string>::iterator iter;  
  12.        iter = mapStudent.find(1);  //mapStudent.count(1);
  13.        if(iter != mapStudent.end())  
  14.       {  
  15.            cout<<"Find, the value is "<<iter->second<<endl;  
  16.        }  
  17.        Else  
  18.        {  
  19.            cout<<"Do not Find"<<endl;  
  20.        }  
  21. }  

 

数据的清空与判空

清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空map

数据的删除

这里要用到erase函数,它有三个重载了的函数,下面在例子中详细说明它们的用法

[cpp] view plain copy

  1. #include <map>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.        map<int, string> mapStudent;  
  8.        mapStudent.insert(pair<int, string>(1, "student_one"));  
  9.        mapStudent.insert(pair<int, string>(2, "student_two"));  
  10.        mapStudent.insert(pair<int, string>(3, "student_three"));  
  11.        //如果你要演示输出效果,请选择以下的一种,你看到的效果会比较好  
  12.        //如果要删除1,用迭代器删除  
  13.        map<int, string>::iterator iter;  
  14.        iter = mapStudent.find(1);  
  15.        mapStudent.erase(iter);  
  16.        //如果要删除1,用关键字删除  
  17.        int n = mapStudent.erase(1);//如果删除了会返回1,否则返回0  
  18.        //用迭代器,成片的删除  
  19.        //一下代码把整个map清空  
  20.        mapStudent.earse(mapStudent.begin(), mapStudent.end());  
  21.        //等同於mapStudent.clear()
  22.        //成片删除要注意的是,也是STL的特性,删除区间是一个前闭后开的集合  
  23. }  

排序

这里要讲的是一点比较高深的用法了,排序问题,STL中默认是采用小于号来排序的,以上代码在排序上是不存在任何问题的,因为上面的关键字是int型,它本身支持小于号运算,在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,因为它没有小于号操作,insert等函数在编译的时候过不去,下面给出两个方法解决这个问题

第一种:小于号重载,程序举例

[cpp] view plain copy

  1. #include <map>  
  2. #include <string>  
  3. uing namespace std;  
  4. Typedef struct tagStudentInfo  
  5. {  
  6.        int      nID;  
  7.        String   strName;  
  8. }StudentInfo, *PStudentInfo;  //学生信息  
  9. int main()  
  10. {  
  11.        int nSize;  
  12.        //用学生信息映射分数  
  13.        map<StudentInfo, int>mapStudent;  
  14.        map<StudentInfo, int>::iterator iter;  
  15.        StudentInfo studentInfo;  
  16.        studentInfo.nID = 1;  
  17.        studentInfo.strName = "student_one"  
  18.        mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));  
  19.        studentInfo.nID = 2;  
  20.        studentInfo.strName = "student_two";  
  21.        mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));  
  22.        for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)  
  23.          cout<<iter->first.nID<<endl<<iter->first.strName<<endl<<iter->second<<endl;  
  24. }  

以上程序是无法编译通过的,只要重载小于号,就OK了,如下:

[cpp] view plain copy

  1. Typedef struct tagStudentInfo  
  2. {  
  3.        int      nID;  
  4.        String   strName;  
  5.        Bool operator < (tagStudentInfo const& _A) const  
  6.        {  
  7.               //这个函数指定排序策略,按nID排序,如果nID相等的话,按strName排序  
  8.               If(nID < _A.nID)  return true;  
  9.               If(nID == _A.nID) return strName.compare(_A.strName) < 0;  
  10.               Return false;  
  11.        }  
  12. }StudentInfo, *PStudentInfo;  //学生信息  

第二种:仿函数的应用,这个时候结构体中没有直接的小于号重载,程序说明

[cpp] view plain copy

  1. #include <map>  
  2. #include <string>  
  3. using namespace std;  
  4. Typedef struct tagStudentInfo  
  5. {  
  6.        int      nID;  
  7.        String   strName;  
  8. }StudentInfo, *PStudentInfo;  //学生信息  
  9. class sort  
  10. {  
  11.        Public:  
  12.        Bool operator() (StudentInfo const &_A, StudentInfo const &_B) const  
  13.        {  
  14.               If(_A.nID < _B.nID) return true;  
  15.               If(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0;  
  16.               Return false;  
  17.        }  
  18. };  
  19. int main()  
  20. {  
  21.        //用学生信息映射分数  
  22.        map<StudentInfo, int, sort>mapStudent;  
  23.        StudentInfo studentInfo;  
  24.        studentInfo.nID = 1;  
  25.        studentInfo.strName = "student_one";  
  26.        mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));  
  27.        studentInfo.nID = 2;  
  28.        studentInfo.strName = "student_two";  
  29.        mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));  
  30. }  

由于STL是一个统一的整体,map的很多用法都和STL中其它的东西结合在一起,比如在排序上,这里默认用的是小于号,即less<>,如果要从大到小排序呢,这里涉及到的东西很多,在此无法一一加以说明。

  1. 还要说明的是,map中由于它内部有序,由红黑树保证,因此很多函数执行的时间复杂度都是log2N的,如果用map函数可以实现的功能,而STL  Algorithm也可以完成该功能,建议用map自带函数,效率高一些。

下面说下,map在空间上的特性,否则,估计你用起来会有时候表现的比较郁闷,由于map的每个数据对应红黑树上的一个节点,这个节点在不保存你的数据时,是占用16个字节的,一个父节点指针,左右孩子指针,还有一个枚举值(标示红黑的,相当于平衡二叉树中的平衡因子),我想大家应该知道,这些地方很费内存了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值