Map is an associative container that contains a sorted list of unique key-value pairs. That list is sorted using the comparison function Compare applied to the keys. Search, removal, and insertion operations have logarithmic complexity. Maps are usually implemented as
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。
下面举例说明什么是一对一的数据映射。比如一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可能轻易描述,很明显学号用int描述,姓名用字符串描述(本篇文章中不用char *来描述字符串,而是采用STL中string来描述),下面给出map描述代码:
Map mapStudent;
1. map的构造函数
map共提供了6个构造函数,这块涉及到内存分配器这些东西,略过不表,在下面我们将接触到一些map的构造方法,这里要说下的就是,我们通常用如下方法构造一个map:
Map mapStudent;
2. 数据的插入
在构造map容器后,我们就可以往里面插入数据了。这里讲三种插入数据的方法:
第一种:用insert函数插入pair数据,下面举例说明
- #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;
-
-
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
-
cout << iter->first << " " << iter->second << endl;
-
-
}
第二种:用insert函数插入value_type数据,下面举例说明
- #include <map>
-
#include <string>
-
#include <iostream>
-
-
using namespace std;
-
-
int main()
-
{
-
-
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;
-
-
}
第三种:用数组方式插入数据,下面举例说明
- #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";
-
-
/* the below will cover the above
-
mapStudent[3] = "student_three_1";
-
*/
-
map<int, string>::iterator iter;
-
-
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
-
cout << iter->first << " " << iter->second << endl;
-
-
cout << "student size:" << mapStudent.size() << endl;
-
}
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值
用pair来获得是否插入成功
- using namespace std;
-
-
-
int main()
-
{
-
-
map<int, string> mapStudent;
-
-
#define MAP_INSERT_CHECK(nr,str) do { \
-
pair < map <int, string>::iterator,bool> InsertPair; \
-
InsertPair = mapStudent.insert(pair<int, string>(nr, str)); \
-
if(InsertPair.second) \
-
cout << "Insert Successfully \n"; \
-
else \
-
cout << "Insert Failure \n" ; \
-
}while(0)
-
-
MAP_INSERT_CHECK(1,"student_one");
-
MAP_INSERT_CHECK(2,"student_two");
-
MAP_INSERT_CHECK(3,"student_three");
-
-
map<int, string>::iterator iter;
-
-
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
-
cout << iter->first << " " << iter->second << endl;
-
-
cout << "student size:" << mapStudent.size() << endl;
-
-
}
上面已经有iterator方式的遍历了,看看数组方式遍历
- #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 size = mapStudent.size();
-
-
for(int i=0; i<size; i++)
-
cout << i+1 << " " << mapStudent[i+1] << endl;
-
-
}
upper_bound,很有意思的东西
- #include <map>
-
#include <string>
-
#include <iostream>
-
-
using namespace std;
-
-
int main()
-
{
-
-
map<int, string> mapStudent;
-
-
mapStudent[1] = "student_one";
-
mapStudent[3] = "student_three";
-
mapStudent[5] = "student_five";
-
-
map<int, string>::iterator iter;
-
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
-
cout << iter->first << " " << iter->second << endl;
-
-
cout << "student size:" << mapStudent.size() << endl;
-
-
cout << "test bound\n" ;
-
-
iter = mapStudent.lower_bound(2);
-
cout <<"lower_bound(2):" <<iter->second << endl;
-
-
iter = mapStudent.upper_bound(2);
-
cout <<"upper_bound(2):" <<iter->second << endl;
-
-
iter = mapStudent.lower_bound(3);
-
cout <<"lower_bound(3):" <<iter->second << endl;
-
-
iter = mapStudent.upper_bound(3);
-
cout <<"upper_bound(3):" <<iter->second << endl;
-
-
-
/* for justifying exiting */
-
pair < map<int,string>::iterator, map<int,string>::iterator > mapPair;
-
mapPair = mapStudent.equal_range(2);
-
if(mapPair.first == mapPair.second)
-
cout <<"key 2 not find\n";
-
else
-
cout <<"key 2 fount\n";
-
-
mapPair = mapStudent.equal_range(3);
-
if(mapPair.first == mapPair.second)
-
cout <<"key 3 not find\n";
-
else
-
cout <<"key 3 fount\n";
-
-
-
}
反向遍历
看清 不是iterator 而是 reverse_iterator,我是看了好久才检查出来的
- #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 riter;
-
-
for(riter = mapStudent.rbegin(); riter != mapStudent.rend(); riter++)
-
cout << riter->first << " " << riter->second << endl;
-
-
}
数据的清空与判空
清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空map
数据的删除
- #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>(3, "student_two"));
-
mapStudent.insert(pair<int, string>(5, "student_three"));
-
-
map<int, string>::iterator iter;
-
-
cout << "\nthe BILL of all student" << endl;
-
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter ++)
-
cout << iter->first << " " << iter->second << endl;
-
cout << "--------------------------" << endl;
-
-
//by iterator
-
iter = mapStudent.find(1);
-
mapStudent.erase(iter);
-
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter ++)
-
cout << "\t" << iter->first << " " << iter->second << endl;
-
-
//by key
-
mapStudent.insert(pair<int, string>(1, "student_one"));
-
cout << "\nthe BILL of all student" << endl;
-
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter ++)
-
cout << iter->first << " " << iter->second << endl;
-
cout << "--------------------------" << endl;
-
-
int rt = 0;
-
rt = mapStudent.erase(1);
-
cout << "erase(1):" << rt << endl;
-
rt = mapStudent.erase(2);
-
cout << "erase(2):" << rt << endl;
-
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter ++)
-
cout << "\t" << iter->first << " " << iter->second << endl;
-
-
//delete a range item
-
mapStudent.insert(pair<int, string>(1, "student_one"));
-
cout << "\nthe BILL of all student" << endl;
-
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter ++)
-
cout << iter->first << " " << iter->second << endl;
-
cout << "--------------------------" << endl;
-
mapStudent.erase(mapStudent.begin(), mapStudent.end());
-
//Removes the elements in the range [first; last).
-
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter ++)
-
cout << "\t" << iter->first << " " << iter->second << endl;
-
}
排序
因为是红黑树存储,本身要有有顺序,因此在构造时就必须明确iterator->first的比较方法,基本数据类型不说了,如果是结构体有两种,一是在结构体或者类重载“<”,二是利用第三个类进行重载“()”进行排序。
- #include <map>
-
#include <iostream>
-
#include <string>
-
using namespace std;
-
typedef struct tagStudentInfo {
-
int nID;
-
string strName;
-
-
/* in map, the sort need "<" for sorting, so this needed */
-
bool operator < (tagStudentInfo const& _A) const {
-
if(nID < _A.nID) return true;
-
if(nID == _A.nID) return strName.compare(_A.strName) < 0;
-
return false;
-
}
-
}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));
-
-
studentInfo.nID = 3;
-
studentInfo.strName = "student_three";
-
mapStudent.insert(pair<StudentInfo, int>(studentInfo, 95));
-
-
cout << "ID\tName\t\tScore\n";
-
for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++ )
-
cout << iter->first.nID <<"\t"
-
<< iter->first.strName <<"\t"
-
<< iter->second <<endl ;
-
}
第二种
- #include <map>
-
#include <iostream>
-
#include <string>
-
using namespace std;
-
typedef struct tagStudentInfo {
-
int nID;
-
string strName;
-
}StudentInfo, *PStudentInfo;
-
-
class BySort{
-
public:
-
bool operator()
-
(StudentInfo const &_A, StudentInfo const &_B) const
-
{
-
if(_A.nID < _B.nID)
-
return true;
-
if(_A.nID == _B.nID)
-
return _A.strName.compare(_B.strName) < 0;
-
return false;
-
}
-
};
-
int main()
-
{
-
int nSize;
-
-
map<StudentInfo, int, BySort>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));
-
-
studentInfo.nID = 3;
-
studentInfo.strName = "student_three";
-
mapStudent.insert(pair<StudentInfo, int>(studentInfo, 95));
-
-
cout << "ID\tName\t\tScore\n";
-
for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++ )
-
cout << iter->first.nID <<"\t"
-
<< iter->first.strName <<"\t"
-
<< iter->second <<endl ;
-
}
参考:
http://www.cnblogs.com/wanghao111/archive/2009/08/10/1542974.html