C++ STL之map

一、简介

  • map是STL 的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。
  • map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。
  • map 是一种有序无重复的关联容器。关联容器与顺序容器不同,他们的元素是按照关键字来保存和访问的,而顺序元素是按照它们在容器中的位置保存和访问的。map保存的是一种 key - value 的pair对象,其中 key 是关键字,value 是关键字对应的值。通过 key找到对应的 value。map中按照 key的大小升序排列pair对象。map会自动建立Key - value的对应。

二、用法

1、头文件

#include <map> 

2、构造函数

//在定义map对象时,必须分别指明键和值的类型。Map建立key-value的一种映射。

//创建一个名为m的空map对象,其键和值的类型分别为key和value
map<key, value> m; 

//创建m2的副本m,m与m2必须有相同的键类型和值类型
map<key, value> m(m2);
 
//创建map类型的对象m,存储迭代器b和e标记的范围内所有元素的副本,元素的类型必须能转换为pair
map<key, value> m(b,e); 

//comp可选,键值对存放策略
map<key, value, comp> mp;

3、map定义的类型

// 在map容器中,用作索引的键的类型
map<key, value> ::key_type; 

// 在map容器中,键所关联的值的类型
map<key, value> ::mapped_type;  

// 一个pair类型,它的first元素具有const map<key, value> ::key_type类型,而second元素则xmap<key, value> :: mapped_type类型。
map<key, value> ::value_type;  

在学习map接口时,谨记其value_type是pair类型,它的值成员可以修改(second成员),但键成员不能修改。这个value_type相当于map的元素类型,而不是键所对应的值的类型。 
对map迭代器进行解引用将产生pair类型的对象,它的first成员存放键,为const,而second成员存放值。

4、插入数据

①用insert函数插入pair数据

  m.insert(pair<int, string>(1, "student_one"));  
make_pair()//返回类型为对应的pair类型  
无需写出类别,就可以生成一个pair对象  
例:  
make_pair(1,'@')  
而不必费力的写成  
pair<int ,char>(1,'@')  

②用insert函数插入value_type数据

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

③数组插入方式

#include <map>  
#include <string>  
#include <iostream>  
using namespace std;  
int main()  
{  
       map<int, string> mapStudent;  
       mapStudent[1] =  "student_one";  
       mapStudent[2] =  "student_two";  
       mapStudent[3] =  "student_three";  
       map<int, string>::iterator  iter;  
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
       {  
          cout<<iter->first<<"   "<<iter->second<<endl;  
       }  
}  

5、map的大小

m.size()查看

6、数据的遍历
①应用前向迭代器

例如4-③中的例子

②应用反向迭代器

#include <map>  
#include <string>  
#include <iostream>  
using namespace std;  
int main()  
{  
       map<int, string> mapStudent;  
       mapStudent.insert(pair<int, string>(1, "student_one"));  
       mapStudent.insert(pair<int, string>(2, "student_two"));  
       mapStudent.insert(pair<int, string>(3, "student_three"));  
       map<int, string>::reverse_iterator  iter;  
       for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)  
       {  
          cout<<iter->first<<"   "<<iter->second<<endl;  
       }  
}  

③用数组方式

#include <map>  
#include <string>  
#include <iostream>  
using namespace std;  
int main()  
{  
       map<int, string> mapStudent;  
       mapStudent.insert(pair<int, string>(1, "student_one"));  
       mapStudent.insert(pair<int, string>(2, "student_two"));  
       mapStudent.insert(pair<int, string>(3, "student_three"));  
       int nSize = mapStudent.size()  
//此处有误,应该是 for(int nIndex = 1; nIndex <= nSize; nIndex++)   
//by rainfish  
       for(int nIndex = 0; nIndex < nSize; nIndex++)  
       {  
           cout<<mapStudent[nIndex]<<end;  
       }  
}  

7、数据的查找

#include <map>  
#include <string>  
#include <iostream>  
using namespace std;  
int main()  
{  
       map<int, string> mapStudent;  
       mapStudent.insert(pair<int, string>(1, "student_one"));  
       mapStudent.insert(pair<int, string>(2, "student_two"));  
       mapStudent.insert(pair<int, string>(3, "student_three"));  
       map<int, string>::iterator iter;  
       iter = mapStudent.find(1);  
       if(iter != mapStudent.end())  
      {  
           cout<<"Find, the value is "<<iter->second<<endl;  
       }  
       Else  
       {  
           cout<<"Do not Find"<<endl;  
       }  
}  

8、排序

#include <map>  
#include <string>  
uing namespace std;  
Typedef struct tagStudentInfo  
{  
       int      nID;  
       String   strName;  
}StudentInfo, *PStudentInfo;  //学生信息  
int main()  
{  
       int nSize;  
       //用学生信息映射分数  
       map<StudentInfo, int>mapStudent;  
       map<StudentInfo, int>::iterator iter;  
       StudentInfo studentInfo;  
       studentInfo.nID = 1;  
       studentInfo.strName = "student_one"  
       mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));  
       studentInfo.nID = 2;  
       studentInfo.strName = "student_two";  
       mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));  
       for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)  
         cout<<iter->first.nID<<endl<<iter->first.strName<<endl<<iter->second<<endl;  
}  

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

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

猜你喜欢

转载自blog.csdn.net/weixin_39731083/article/details/81316523