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;
}