map(1)

来源:互联网 发布:绿坝软件下载 编辑:程序博客网 时间:2024/06/05 07:35

map 是 STL 的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在 map 中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里简单说一下 map 内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在 map 内部所有的数据都是有序的,后边我们会见识到有序的好处。


那什么是一对一的数据映射?比如一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用 map 可能轻易描述,很明显学号用 int 描述,姓名用字符串描述。


使用 map 之前,必须包含相应的头文件,map 属于 std 命名域的,因此需要通过命名限定:

#include <map>

using std::map; //using namespace std;


1)map 的构造函数

map 共提供了 6 个构造函数,这块涉及到内存分配器这些东西,略过不讲,在下面我们将接触到一些 map 的构造方法,我们通常用如下方法构造一个 map :

[cpp] view plain copy
  1. map<int, string> mapStudent;  

2) 数据的插入

在构造 map 容器后,我们就可以往里面插入数据。这里讲三种插入数据的方法:

a)用 insert 函数插入 pair 数据:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <map>    //map  
  3. #include <string> //string  
  4. #include <utility> //pair  
  5.   
  6. using std::endl;  
  7. using std::cin;  
  8. using std::cout;  
  9. using std::map;  
  10. using std::string;  
  11. using std::pair;  
  12. //using namespace std;//或直接把所有都包含  
  13.   
  14. //用insert函数插入pair数据  
  15. int main(int argc, char *argv[])  
  16. {  
  17.       
  18.     map<int, string> mapStudent;  
  19.       
  20.     //用insert函数插入pair数据  
  21.     mapStudent.insert( pair<int, string>(1, "student_one") );  
  22.     mapStudent.insert( pair<int, string>(2, "student_two") );  
  23.     mapStudent.insert( pair<int, string>(3, "student_three") );  
  24.       
  25.     //通过迭代器遍历map的内容  
  26.     map<int, string>::iterator iter;  
  27.     for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  28.     {  
  29.         cout<<iter->first<<" "<<iter->second<<endl;  
  30.     }  
  31.   
  32.     return 0;  
  33. }  

运行结果如下:



b)用 insert 函数插入 value_type 数据:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <map>    //map pair  
  3. #include <string> //string  
  4. #include <utility> //pair  
  5.   
  6. using std::endl;  
  7. using std::cin;  
  8. using std::cout;  
  9. using std::map;  
  10. using std::string;  
  11. using std::pair;  
  12. //using namespace std;//或直接把所有都包含  
  13.   
  14. //用insert函数插入value_type数据  
  15. int main(int argc, char *argv[])  
  16. {  
  17.       
  18.     map<int, string> mapStudent;  
  19.       
  20.     //用insert函数插入value_type数据  
  21.     mapStudent.insert( map<int, string>::value_type(1, "student_one") );  
  22.     mapStudent.insert( map<int, string>::value_type(2, "student_two") );  
  23.     mapStudent.insert( map<int, string>::value_type(3, "student_three") );  
  24.       
  25.     //通过迭代器遍历map的内容  
  26.     map<int, string>::iterator iter;  
  27.     for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  28.     {  
  29.         cout<<iter->first<<" "<<iter->second<<endl;  
  30.     }  
  31.   
  32.     return 0;  
  33. }  

c)用数组方式插入数据:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <map>    //map pair  
  3. #include <string> //string  
  4.   
  5. using std::endl;  
  6. using std::cin;  
  7. using std::cout;  
  8. using std::map;  
  9. using std::string;  
  10. using std::pair;  
  11. //using namespace std;//或直接把所有都包含  
  12.   
  13. //用数组方式插入数据  
  14. int main(int argc, char *argv[])  
  15. {  
  16.       
  17.     map<int, string> mapStudent;  
  18.       
  19.     //用数组方式插入数据  
  20.     mapStudent[0] = "student_one";  
  21.     mapStudent[2] = "student_two";  
  22.     mapStudent[3] = "student_three";  
  23.       
  24.     //通过迭代器遍历map的内容  
  25.     map<int, string>::iterator iter;  
  26.     for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  27.     {  
  28.         cout<<iter->first<<" "<<iter->second<<endl;  
  29.     }  
  30.   
  31.     return 0;  
  32. }  

运行结果如下:


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


那我们怎么知道 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。


完整测试代码如下:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <map>    //map pair  
  3. #include <string> //string  
  4.   
  5. using std::endl;  
  6. using std::cin;  
  7. using std::cout;  
  8. using std::map;  
  9. using std::string;  
  10. using std::pair;  
  11. //using namespace std;//或直接把所有都包含  
  12.   
  13. //测试插入是否成功  
  14. int main(int argc, char *argv[])  
  15. {  
  16.       
  17.     map<int, string> mapStudent;  
  18.       
  19.     pair<map<int, string>::iterator, bool> insert_pair;  
  20.   
  21.     //插入数据  
  22.     insert_pair = mapStudent.insert(pair<int, string>(1, "student_one"));  
  23.     if(insert_pair.second == true)//插入成功  
  24.     {  
  25.         cout << "Insert Successfully" << endl;  
  26.     }  
  27.     else  
  28.     {  
  29.         cout << "Insert Failure" << endl;  
  30.     }  
  31.       
  32.     //插入数据  
  33.     insert_pair = mapStudent.insert(pair<int, string>(1, "student_two"));  
  34.     if(insert_pair.second == true)//插入成功  
  35.     {  
  36.         cout << "Insert Successfully" << endl;  
  37.     }  
  38.     else  
  39.     {  
  40.         cout << "Insert Failure" << endl;  
  41.     }  
  42.       
  43.     cout << "\n------------------------\n";  
  44.     //通过迭代器遍历map的内容  
  45.     map<int, string>::iterator iter;  
  46.     for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  47.     {  
  48.         cout<<iter->first<<" "<<iter->second<<endl;  
  49.     }  
  50.   
  51.     return 0;  
  52. }  

运行结果如下:



下面示例为测试数组赋值,数据覆盖问题:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <map>    //map pair  
  3. #include <string> //string  
  4.   
  5. using std::endl;  
  6. using std::cin;  
  7. using std::cout;  
  8. using std::map;  
  9. using std::string;  
  10. using std::pair;  
  11. //using namespace std;//或直接把所有都包含  
  12.   
  13. //测试数组赋值,数据覆盖问题  
  14. int main(int argc, char *argv[])  
  15. {  
  16.       
  17.     map<int, string> mapStudent;  
  18.       
  19.     mapStudent[1] = "student_one";  
  20.     mapStudent[1] = "student_two";  //这个也是 1  
  21.     mapStudent[2] = "student_three";  
  22.       
  23.     cout << "\n------------------------\n";  
  24.     //通过迭代器遍历map的内容  
  25.     map<int, string>::iterator iter;  
  26.     for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  27.     {  
  28.         cout<<iter->first<<" "<<iter->second<<endl;  
  29.     }  
  30.   
  31.     return 0;  
  32. }  

运行结果如下:



3)map 的大小

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

[cpp] view plain copy
  1. int nSize = mapStudent.size();  

4)数据的遍历

a)通过前向迭代器,上面举例程序中就是使用此方式。


b)通过反相迭代器:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <map>    //map pair  
  3. #include <string> //string  
  4.   
  5. using std::endl;  
  6. using std::cin;  
  7. using std::cout;  
  8. using std::map;  
  9. using std::string;  
  10. using std::pair;  
  11. //using namespace std;//或直接把所有都包含  
  12.   
  13. //使用反相迭代器遍历map内容  
  14. int main(int argc, char *argv[])  
  15. {  
  16.       
  17.     map<int, string> mapStudent;  
  18.       
  19.     //用insert函数插入pair数据  
  20.     mapStudent.insert( pair<int, string>(1, "student_one") );  
  21.     mapStudent.insert( pair<int, string>(2, "student_two") );  
  22.     mapStudent.insert( pair<int, string>(3, "student_three") );  
  23.       
  24.     //cout << "\n------------------------\n";  
  25.     //使用反相迭代器遍历map内容  
  26.     map<int, string>::reverse_iterator iter;  
  27.     for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)  
  28.     {  
  29.         cout<<iter->first<<" "<<iter->second<<endl;  
  30.     }  
  31.   
  32.     return 0;  
  33. }  


运行结果如下:



c)通过数组方式:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <map>    //map pair  
  3. #include <string> //string  
  4.   
  5. using std::endl;  
  6. using std::cin;  
  7. using std::cout;  
  8. using std::map;  
  9. using std::string;  
  10. using std::pair;  
  11. //using namespace std;//或直接把所有都包含  
  12.   
  13. //使用数组方式遍历map内容  
  14. int main(int argc, char *argv[])  
  15. {  
  16.       
  17.     map<int, string> mapStudent;  
  18.       
  19.     //用insert函数插入pair数据  
  20.     mapStudent.insert( pair<int, string>(1, "student_one") );  
  21.     mapStudent.insert( pair<int, string>(2, "student_two") );  
  22.     mapStudent.insert( pair<int, string>(3, "student_three") );  
  23.       
  24.     //cout << "\n------------------------\n";  
  25.     //使用数组方式遍历map内容  
  26.     int n = mapStudent.size();  
  27.     for(int i = 1; i <= n; i++) //从1开始  
  28.     {  
  29.         cout<< i << " " << mapStudent[i]<<endl;  
  30.     }  
  31.   
  32.     return 0;  
  33. }  

5)数据的查找

可以使用 find 函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果 map 中没有要查找的数据,它返回的迭代器等于 end 函数返回的迭代器。示例代码如下:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <map>    //map pair  
  3. #include <string> //string  
  4.   
  5. using std::endl;  
  6. using std::cin;  
  7. using std::cout;  
  8. using std::map;  
  9. using std::string;  
  10. using std::pair;  
  11. //using namespace std;//或直接把所有都包含  
  12.   
  13. //用find函数来定位数据出现位置  
  14. int main(int argc, char *argv[])  
  15. {  
  16.       
  17.     map<int, string> mapStudent;  
  18.       
  19.     //用insert函数插入pair数据  
  20.     mapStudent.insert( pair<int, string>(1, "student_one") );  
  21.     mapStudent.insert( pair<int, string>(2, "student_two") );  
  22.     mapStudent.insert( pair<int, string>(3, "student_three") );  
  23.       
  24.     map<int, string>::iterator iter;  
  25.   
  26.     iter = mapStudent.find(1); //查找 1 的内容  
  27.       
  28.     if( iter != mapStudent.end() ) //找到  
  29.     {  
  30.         cout << "Find, the value is: "<< iter->second << endl;  
  31.       
  32.     }  
  33.     else  
  34.     {  
  35.         cout << "Do not Find" << endl;  
  36.     }  
  37.   
  38.     return 0;  
  39. }  

运行结果如下:



6)数据的清空与判空

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


7)数据的删除

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

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <map>    //map pair  
  3. #include <string> //string  
  4.   
  5. using std::endl;  
  6. using std::cin;  
  7. using std::cout;  
  8. using std::map;  
  9. using std::string;  
  10. using std::pair;  
  11. //using namespace std;//或直接把所有都包含  
  12.   
  13. //数据的删除  
  14. int main(int argc, char *argv[])  
  15. {  
  16.       
  17.     map<int, string> mapStudent;  
  18.       
  19.     //用insert函数插入pair数据  
  20.     mapStudent.insert( pair<int, string>(0, "student_zero") );  
  21.     mapStudent.insert( pair<int, string>(1, "student_one") );  
  22.     mapStudent.insert( pair<int, string>(2, "student_two") );  
  23.     mapStudent.insert( pair<int, string>(3, "student_three") );  
  24.     mapStudent.insert( pair<int, string>(4, "student_four") );  
  25.       
  26.     //如果要删除1,用迭代器删除  
  27.     map<int, string>::iterator iter;  
  28.     iter = mapStudent.find(1); //先查找1  
  29.     mapStudent.erase(iter);  
  30.       
  31.     cout << "\nafter erase 1:\n";  
  32.     //通过迭代器遍历map的内容  
  33.     for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  34.     {  
  35.         cout<<iter->first<<" "<<iter->second<<endl;  
  36.     }  
  37.       
  38.       
  39.     //如果要删除2,可用关键字删除  
  40.     int n = mapStudent.erase(2);//如果删除成功会返回1,否则返回0  
  41.     cout << "n = " << n << endl;  
  42.       
  43.     cout << "\nafter erase 2:\n";  
  44.     //通过迭代器遍历map的内容  
  45.     for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  46.     {  
  47.         cout<<iter->first<<" "<<iter->second<<endl;  
  48.     }  
  49.       
  50.     //用迭代器,成片的删除  
  51.     //成片删除要注意的是,也是STL的特性,删除区间是一个前闭后开的集合  
  52.     mapStudent.erase( mapStudent.begin(), mapStudent.end() );  
  53.       
  54.     cout << "\nafter erase all:\n";  
  55.     //通过迭代器遍历map的内容  
  56.     for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  57.     {  
  58.         cout<<iter->first<<" "<<iter->second<<endl;  
  59.     }  
  60.   
  61.     return 0;  
  62. }  

运行结果如下:



8)排序 

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

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <map>    //map pair  
  3. #include <string> //string  
  4.   
  5. using std::endl;  
  6. using std::cin;  
  7. using std::cout;  
  8. using std::map;  
  9. using std::string;  
  10. using std::pair;  
  11. //using namespace std;//或直接把所有都包含  
  12.   
  13. typedef struct tagStudentInfo  
  14. {  
  15.     int nID;  
  16.     string strName;  
  17. }StudentInfo, *PStudentInfo; //学生信息  
  18.   
  19. //数据的删除  
  20. int main(int argc, char *argv[])  
  21. {  
  22.       
  23.     //用学生信息映射分数  
  24.     map<StudentInfo, int> mapStudent;  
  25.   
  26.     StudentInfo studentInfo;  
  27.     studentInfo.nID = 1;  
  28.     studentInfo.strName = "student_one";  
  29.     mapStudent.insert( pair<StudentInfo, int>(studentInfo, 90) );  
  30.   
  31.     studentInfo.nID = 2;  
  32.     studentInfo.strName = "student_two";  
  33.     mapStudent.insert( pair<StudentInfo, int>(studentInfo, 80) );  
  34.   
  35.    
  36.     map<StudentInfo, int>::iterator iter;  
  37.     for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  38.     {  
  39.         cout<<iter->first.nID<<" "<<iter->first.strName<<" "<<iter->second<<endl;  
  40.     }  
  41.   
  42.     return 0;  
  43. }  

以上程序是无法编译通过的

解决方法为:重载小于号。示例如下:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <map>    //map pair  
  3. #include <string> //string  
  4.   
  5. using std::endl;  
  6. using std::cin;  
  7. using std::cout;  
  8. using std::map;  
  9. using std::string;  
  10. using std::pair;  
  11. //using namespace std;//或直接把所有都包含  
  12.   
  13. typedef struct tagStudentInfo  
  14. {  
  15.     int nID;  
  16.     string strName;  
  17.       
  18.     bool operator< (tagStudentInfo const &temp) const  
  19.     {  
  20.         //这个函数指定排序策略,按nID排序,如果nID相等的话,按strName排序  
  21.         if(nID < temp.nID)  return true;  
  22.           
  23.         if(nID == temp.nID) return strName.compare(temp.strName);  
  24.           
  25.         return false;  
  26.     }  
  27.       
  28. }StudentInfo, *PStudentInfo; //学生信息  
  29.   
  30. //数据的删除  
  31. int main(int argc, char *argv[])  
  32. {  
  33.       
  34.     //用学生信息映射分数  
  35.     map<StudentInfo, int> mapStudent;  
  36.   
  37.     StudentInfo studentInfo;  
  38.     studentInfo.nID = 1;  
  39.     studentInfo.strName = "student_one";  
  40.     mapStudent.insert( pair<StudentInfo, int>(studentInfo, 90) );  
  41.   
  42.     studentInfo.nID = 2;  
  43.     studentInfo.strName = "student_two";  
  44.     mapStudent.insert( pair<StudentInfo, int>(studentInfo, 80) );  
  45.   
  46.    
  47.     map<StudentInfo, int>::iterator iter;  
  48.     for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  49.     {  
  50.         cout<<iter->first.nID<<" "<<iter->first.strName<<" "<<iter->second<<endl;  
  51.     }  
  52.   
  53.     return 0;  
  54. }  

运行结果图如下:



本教程示例代码下载请点此链接:http://download.csdn.net/detail/tennysonsky


本文转自:http://www.kuqin.com/cpluspluslib

原创粉丝点击