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 :
- map<int, string> mapStudent;
2) 数据的插入
在构造 map 容器后,我们就可以往里面插入数据。这里讲三种插入数据的方法:
a)用 insert 函数插入 pair 数据:
- #include <iostream>
- #include <map> //map
- #include <string> //string
- #include <utility> //pair
-
- using std::endl;
- using std::cin;
- using std::cout;
- using std::map;
- using std::string;
- using std::pair;
-
-
-
- int main(int argc, char *argv[])
- {
-
- 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;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
-
- return 0;
- }
运行结果如下:
b)用 insert 函数插入 value_type 数据:
- #include <iostream>
- #include <map> //map pair
- #include <string> //string
- #include <utility> //pair
-
- using std::endl;
- using std::cin;
- using std::cout;
- using std::map;
- using std::string;
- using std::pair;
-
-
-
- int main(int argc, char *argv[])
- {
-
- map<int, string> mapStudent;
-
-
- mapStudent.insert( map<int, string>::value_type(1, "student_one") );
- mapStudent.insert( map<int, string>::value_type(2, "student_two") );
- mapStudent.insert( map<int, string>::value_type(3, "student_three") );
-
-
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
-
- return 0;
- }
c)用数组方式插入数据:
- #include <iostream>
- #include <map> //map pair
- #include <string> //string
-
- using std::endl;
- using std::cin;
- using std::cout;
- using std::map;
- using std::string;
- using std::pair;
-
-
-
- int main(int argc, char *argv[])
- {
-
- map<int, string> mapStudent;
-
-
- mapStudent[0] = "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;
- }
-
- return 0;
- }
运行结果如下:
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的。第一种和第二种在效果上是完成一样的,用 insert 函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当 map 中已经存在某个关键字时,insert 操作是不能成功插入数据,但是用数组方式则可以,它可以覆盖以前该关键字对应的值。
那我们怎么知道 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。
完整测试代码如下:
- #include <iostream>
- #include <map> //map pair
- #include <string> //string
-
- using std::endl;
- using std::cin;
- using std::cout;
- using std::map;
- using std::string;
- using std::pair;
-
-
-
- int main(int argc, char *argv[])
- {
-
- map<int, string> mapStudent;
-
- pair<map<int, string>::iterator, bool> insert_pair;
-
-
- insert_pair = mapStudent.insert(pair<int, string>(1, "student_one"));
- if(insert_pair.second == true)
- {
- cout << "Insert Successfully" << endl;
- }
- else
- {
- cout << "Insert Failure" << endl;
- }
-
-
- insert_pair = mapStudent.insert(pair<int, string>(1, "student_two"));
- if(insert_pair.second == true)
- {
- cout << "Insert Successfully" << endl;
- }
- else
- {
- cout << "Insert Failure" << endl;
- }
-
- cout << "\n------------------------\n";
-
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
-
- return 0;
- }
运行结果如下:
下面示例为测试数组赋值,数据覆盖问题:
- #include <iostream>
- #include <map> //map pair
- #include <string> //string
-
- using std::endl;
- using std::cin;
- using std::cout;
- using std::map;
- using std::string;
- using std::pair;
-
-
-
- int main(int argc, char *argv[])
- {
-
- map<int, string> mapStudent;
-
- mapStudent[1] = "student_one";
- mapStudent[1] = "student_two";
- mapStudent[2] = "student_three";
-
- cout << "\n------------------------\n";
-
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
-
- return 0;
- }
运行结果如下:
3)map 的大小
在往 map 里面插入了数据,我们怎么知道当前已经插入了多少数据呢,可以用 size 函数,用法如下:
- int nSize = mapStudent.size();
4)数据的遍历
a)通过前向迭代器,上面举例程序中就是使用此方式。
b)通过反相迭代器:
- #include <iostream>
- #include <map> //map pair
- #include <string> //string
-
- using std::endl;
- using std::cin;
- using std::cout;
- using std::map;
- using std::string;
- using std::pair;
-
-
-
- int main(int argc, char *argv[])
- {
-
- 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;
- }
-
- return 0;
- }
运行结果如下:
c)通过数组方式:
- #include <iostream>
- #include <map> //map pair
- #include <string> //string
-
- using std::endl;
- using std::cin;
- using std::cout;
- using std::map;
- using std::string;
- using std::pair;
-
-
-
- int main(int argc, char *argv[])
- {
-
- 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 n = mapStudent.size();
- for(int i = 1; i <= n; i++)
- {
- cout<< i << " " << mapStudent[i]<<endl;
- }
-
- return 0;
- }
5)数据的查找
可以使用 find 函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果 map 中没有要查找的数据,它返回的迭代器等于 end 函数返回的迭代器。示例代码如下:
- #include <iostream>
- #include <map> //map pair
- #include <string> //string
-
- using std::endl;
- using std::cin;
- using std::cout;
- using std::map;
- using std::string;
- using std::pair;
-
-
-
- int main(int argc, char *argv[])
- {
-
- 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;
- }
-
- return 0;
- }
运行结果如下:
6)数据的清空与判空
清空 map 中的数据可以用 clear() 函数,判定 map 中是否有数据可以用 empty() 函数,它返回 true 则说明是空 map。
7)数据的删除
这里要用到 erase 函数,它有三个重载了的函数,下面在例子中详细说明它们的用法:
- #include <iostream>
- #include <map> //map pair
- #include <string> //string
-
- using std::endl;
- using std::cin;
- using std::cout;
- using std::map;
- using std::string;
- using std::pair;
-
-
-
- int main(int argc, char *argv[])
- {
-
- map<int, string> mapStudent;
-
-
- mapStudent.insert( pair<int, string>(0, "student_zero") );
- mapStudent.insert( pair<int, string>(1, "student_one") );
- mapStudent.insert( pair<int, string>(2, "student_two") );
- mapStudent.insert( pair<int, string>(3, "student_three") );
- mapStudent.insert( pair<int, string>(4, "student_four") );
-
-
- map<int, string>::iterator iter;
- iter = mapStudent.find(1);
- mapStudent.erase(iter);
-
- cout << "\nafter erase 1:\n";
-
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
-
-
-
- int n = mapStudent.erase(2);
- cout << "n = " << n << endl;
-
- cout << "\nafter erase 2:\n";
-
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
-
-
-
- mapStudent.erase( mapStudent.begin(), mapStudent.end() );
-
- cout << "\nafter erase all:\n";
-
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
-
- return 0;
- }
运行结果如下:
8)排序
这里要讲的是一点比较高深的用法:排序问题,STL 中默认是采用小于号来排序的,以上代码在排序上是不存在任何问题的,因为上面的关键字是 int 型,它本身支持小于号运算,在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,因为它没有小于号操作,insert 等函数在编译的时候过不去,测试代码如下:
- #include <iostream>
- #include <map> //map pair
- #include <string> //string
-
- using std::endl;
- using std::cin;
- using std::cout;
- using std::map;
- using std::string;
- using std::pair;
-
-
- typedef struct tagStudentInfo
- {
- int nID;
- string strName;
- }StudentInfo, *PStudentInfo;
-
-
- int main(int argc, char *argv[])
- {
-
-
- map<StudentInfo, int> mapStudent;
-
- 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) );
-
-
- map<StudentInfo, int>::iterator iter;
- for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first.nID<<" "<<iter->first.strName<<" "<<iter->second<<endl;
- }
-
- return 0;
- }
以上程序是无法编译通过的。
解决方法为:重载小于号。示例如下:
- #include <iostream>
- #include <map> //map pair
- #include <string> //string
-
- using std::endl;
- using std::cin;
- using std::cout;
- using std::map;
- using std::string;
- using std::pair;
-
-
- typedef struct tagStudentInfo
- {
- int nID;
- string strName;
-
- bool operator< (tagStudentInfo const &temp) const
- {
-
- if(nID < temp.nID) return true;
-
- if(nID == temp.nID) return strName.compare(temp.strName);
-
- return false;
- }
-
- }StudentInfo, *PStudentInfo;
-
-
- int main(int argc, char *argv[])
- {
-
-
- map<StudentInfo, int> mapStudent;
-
- 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) );
-
-
- map<StudentInfo, int>::iterator iter;
- for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first.nID<<" "<<iter->first.strName<<" "<<iter->second<<endl;
- }
-
- return 0;
- }
运行结果图如下:
本教程示例代码下载请点此链接:http://download.csdn.net/detail/tennysonsky
本文转自:http://www.kuqin.com/cpluspluslib