map容器

来源:互联网 发布:甄嬛传演技知乎 编辑:程序博客网 时间:2024/05/01 14:53

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

1. map中的元素其实就是一个pair.
2. map的键一般不能是指针, 比如int*, char*之类的, 会出错. 常用的就用string了,int也行.
3. map是个无序的容器, 而vector之类是有序的. 所谓有序无序是指放入的元素并不是按一定顺序放进去的, 而是乱序, 随机存放的(被映射后近似随机存放).所以遍历的时候有些效率差别.
4. 判断有没有找到该键的内容可以这样:
std::map<std::string,Record>::const_iterator cIter;
cIter = stdfile.m_map.find(s);
if (cIter == stdfile.m_map.end()) // 没找到就是指向END了  
{
m_vecMoreFile.push_back(s);
}
如果键的内容是指针的话, 应该用NULL指针也可以判断了.
5. 遍历:
std::map<std::string,Record>::iterator iter;
for (iter = m_map.begin(); iter != m_map.end(); iter++)
{
std::string s = iter->second.filename;
}
由于map内容可以相当一个PAIR, 那就简单了, 用iter->second就可以取得值了.

可顺便转个其它的几种用法:
1 头文件
#include <map>

2 定义
map<string, int> my_Map;
或者是typedef map<string, int> MY_MAP;
MY_MAP my_Map;

3 插入数据
(1) my_Map["a"] = 1;
(2) my_Map.insert(map<string, int>::value_type("b",2));
(3) my_Map.insert(pair<string,int>("c",3));
(4) my_Map.insert(make_pair("d",4));

4 查找数据和修改数据
(1) int i = my_Map["a"];
my_Map["a"] = i;
(2) MY_MAP::iterator my_Itr;
my_Itr.find("b");
int j = my_Itr->second;
my_Itr->second = j;
不过注意,键本身是不能被修改的,除非删除。

5 删除数据
(1) my_Map.erase(my_Itr);
(2) my_Map.erase("c");
还是注意,第一种情况在迭代期间是不能被删除的,道理和foreach时不能删除元素一样。

6 迭代数据
for (my_Itr=my_Map.begin(); my_Itr!=my_Map.end(); ++my_Itr) {}

7 其它方法
my_Map.size() 返回元素数目
my_Map.empty() 判断是否为空
my_Map.clear() 清空所有元素
可以直接进行赋值和比较:=, >, >=, <, <=, != 等等

遍历:

#include<iostream>
#include <map>
using namespace std;

int main(){
map<int,int>M;
M[1]=2;
M[2]=3;
map<int,int>::iterator iter;
for (iter = M.begin(); iter != M.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
return 0;
}

通常在使用STL时,除了List和vector,我们还会经常用到Map。
Map使用关键值_Key来唯一标识每一个成员_Tp。

STL中的Map声明如下:
template < class _Key, class _Tp, class _Compare = less<_Key>, class _Alloc = allocator<pair<const _Key, _Tp> > > class map { ... }
其中_Compare是用来对_Tp进行排序用的,以便提高查询效率。
缺省是less<_Key>
原型如下:
template <class _Tp> struct less : public binary_function<_Tp,_Tp,bool> { bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; } };
它是一个Functor,留给Map排序用。不过平时我们也可以使用它:
例:
std::less<int> MyLess; bool bRetVal = MyLess(3, 12); cout << ((bRetVal == true) ? "Less" : "Greater") << endl;
OK,下面就来示范一个:

#include<map> #include<iostream>   using namespace std;   typedef map<int, string, less<int> > M_TYPE; typedef M_TYPE::iterator M_IT; typedef M_TYPE::const_iterator M_CIT;   int main() { M_TYPE MyTestMap; MyTestMap[3] = "No.3"; MyTestMap[5] = "No.5"; MyTestMap[1] = "No.1"; MyTestMap[2] = "No.2"; MyTestMap[4] = "No.4"; M_IT it_stop = MyTestMap.find(2); cout << "MyTestMap[2] = " << it_stop->second << endl; it_stop->second = "No.2 After modification"; cout << "MyTestMap[2] = " << it_stop->second << endl; cout << "Map contents : " << endl; for(M_CIT it = MyTestMap.begin(); it != MyTestMap.end(); it++) { cout << it->second << endl; } return 0; }
程序输出:

MyTestMap[2] = No.2
MyTestMap[2] = No.2 After modification
Map contents :
No.1
No.2 After modification
No.3
No.4
No.5

可见Map已经根据less<int>对各个成员_Tp进行从小到大进行排了序;
同理,若我们不使用less,改而使用greater<int>,则Map将成员从大到小排序。

/****************CMAP用法*********************/
#include "stdafx.h"
#include <iostream>
using namespace std;
class Point
{
public:
Point()
{
m_x = 0;
m_y = 0;
}
Point(int x, int y)
{
m_x = x;
m_y = y;
}
public:
int m_x;
int m_y;
};
typedef CMap<const char*, const char*, Point, Point&>     CMapPnt; //请在使用之前定义
int main()
{
Point elem1(1, 100), elem2(2, 200), elem3(3, 300), point;
CMapPnt mp;
// insert 3 elements into map,          #1
mp.SetAt("1st", elem1);
mp.SetAt("2nd", elem2);
mp.SetAt("3th", elem3);
// search a point named "2nd" from map                  #2
mp.Lookup("2nd", point);
printf("2nd: m_x: %d, m_y: %d\n", point.m_x, point.m_y);
// insert a new pair into map      #3
Point elem4(4, 400);
mp["4th"] = elem4;
cout<<"count: "<<mp.GetCount()<<endl;
// traverse the entire map                    #4
size_t index = 0;
const char* pszKey;
POSITION ps = mp.GetStartPosition();
while( ps )
{  
mp.GetNextAssoc(ps, pszKey, point);
printf("index: %d, m_x: %d, m_y: %d\n", ++index, point.m_x, point.m_y);
}
return 0;
}

原创粉丝点击