1、map簡介
map是一類關聯式容器。它的特點是增加和刪除節點對迭代器的影響很小,除了那個操作節點,對其他的節點都沒有什麼影響。對於迭代器來說,可以修改實值,而不能修改key。
2、map的功能
自動建立Key - value的對應。key 和 value可以是任意你需要的類型。
根據key值快速查找記錄,查找的復雜度基本是Log(N),如果有1000個記錄,最多查找10次,1,000,000個記錄,最多查找20次。
快速插入Key - Value 記錄。
快速刪除記錄
根據Key 修改value記錄。
遍歷所有記錄。
3、使用map
使用map得包含map類所在的頭文件
#include <map> //注意,STL頭文件沒有擴展名.h
map對象是模板類,需要關鍵字和存儲對象兩個模板參數:
std:map<int, string> personnel;
這樣就定義了一個用int作為索引,並擁有相關聯的指向string的指針.
為了使用方便,可以對模板類進行一下類型定義,
typedef map<int, CString> UDT_MAP_INT_CSTRING;
UDT_MAP_INT_CSTRING enumMap;
4、在map中插入元素
改變map中的條目非常簡單,因為map類已經對[]操作符進行了重載
enumMap[1] = "One";
enumMap[2] = "Two";
.....
這樣非常直觀,但存在一個性能的問題。插入2時,先在enumMap中查找主鍵為2的項,沒發現,然後將一個新的對象插入enumMap,鍵是2,值是一個空字符串,插入完成後,將字符串賦為"Two"; 該方法會將每個值都賦為缺省值,然後再賦為顯示的值,如果元素是類對象,則開銷比較大。我們可以用以下方法來避免開銷:
enumMap.insert(map<int, CString> :: value_type(2, "Two"))
5、查找並獲取map中的元素
下標操作符給出了獲得一個值的最簡單方法:
CString tmp = enumMap[2];
但是,只有當map中有這個鍵的實例時才對,否則會自動插入一個實例,值為初始化值。
我們可以使用Find()和Count()方法來發現一個鍵是否存在。
查找map中是否包含某個關鍵字條目用find()方法,傳入的參數是要查找的key,在這裡需要提到的是begin()和end()兩個成員,分別代表map對象中第一個條目和最後一個條目,這兩個數據的類型是iterator.
int nFindKey = 2; //要查找的Key
//定義一個條目變量(實際是指針)
UDT_MAP_INT_CSTRING::iterator it= enumMap.find(nFindKey);
if(it == enumMap.end())
{
//沒找到
}
else
{
//找到
}
通過map對象的方法獲取的iterator數據類型是一個std::pair對象,包括兩個數據 iterator->first 和 iterator->second 分別代表關鍵字和存儲的數據
6、從map中刪除元素
移除某個map中某個條目用erase()
該成員方法的定義如下
iterator erase(iterator it); //通過一個條目對象刪除
iterator erase(iterator first, iterator last); //刪除一個范圍
size_type erase(const Key& key); //通過關鍵字刪除
clear()就相當於 enumMap.erase(enumMap.begin(), enumMap.end());
C++ STL map的使用
以下是對C++中STL map的插入,查找,遍歷及刪除的例子:
[cpp]
#include <map>
#include <string>
#include <iostream>
using namespace std;
void map_insert(map < string, string > *mapStudent, string index, string x)
{
mapStudent->insert(map < string, string >::value_type(index, x));
}
int main(int argc, char **argv)
{
char tmp[32] = "";
map < string, string > mapS;
//insert element
map_insert(&mapS, "192.168.0.128", "uestc");
map_insert(&mapS, "192.168.200.3", "EE");
map_insert(&mapS, "192.168.200.33", "CS");
map < string, string >::iterator iter;
cout << "We Have Third Element:" << endl;
cout << "-----------------------------" << endl;
//find element
iter = mapS.find("192.168.0.33");
if (iter != mapS.end())
{
cout << "find the elememt" << endl;
cout << "It is:" << iter->second << endl;
}
else
{
cout << "not find the element" << endl;
}
//see element
for (iter = mapS.begin(); iter != mapS.end(); iter++ )
{
cout << "| " << iter->first << " | " << iter->
second << " |" << endl;
}
cout << "-----------------------------" << endl;
map_insert(&mapS, "192.168.30.23", "CT");
cout << "After We Insert One Element:" << endl;
cout << "-----------------------------" << endl;
for (iter = mapS.begin(); iter != mapS.end(); iter++ )
{
cout << "| " << iter->first << " | " << iter->
second << " |" << endl;
}
cout << "-----------------------------" << endl;
//delete element
iter = mapS.find("192.168.200.33");
if (iter != mapS.end())
{
cout << "find the element:" << iter->first << endl;
cout << "delete element:" << iter->first << endl;
cout << "=================================" << endl;
mapS.erase(iter);
}
else
{
cout << "not find the element" << endl;
}
for (iter = mapS.begin(); iter != mapS.end(); iter++ )
{
cout << "| " << iter->first << " | " << iter->
second << " |" << endl;
}
cout << "=================================" << endl;
return 0;
}
/***************
We Have Third Element:
-----------------------------
not find the element
| 192.168.0.128 | uestc |
| 192.168.200.3 | EE |
| 192.168.200.33 | CS |
-----------------------------
After We Insert One Element:
-----------------------------
| 192.168.0.128 | uestc |
| 192.168.200.3 | EE |
| 192.168.200.33 | CS |
| 192.168.30.23 | CT |
-----------------------------
find the element:192.168.200.33
delete element:192.168.200.33
=================================
| 192.168.0.128 | uestc |
| 192.168.200.3 | EE |
| 192.168.30.23 | CT |
=================================
Process returned 0 (0x0) execution time : 1.080 s
Press any key to continue.
*****************/
#include <map>
#include <string>
#include <iostream>
using namespace std;
void map_insert(map < string, string > *mapStudent, string index, string x)
{
mapStudent->insert(map < string, string >::value_type(index, x));
}
int main(int argc, char **argv)
{
char tmp[32] = "";
map < string, string > mapS;
//insert element
map_insert(&mapS, "192.168.0.128", "uestc");
map_insert(&mapS, "192.168.200.3", "EE");
map_insert(&mapS, "192.168.200.33", "CS");
map < string, string >::iterator iter;
cout << "We Have Third Element:" << endl;
cout << "-----------------------------" << endl;
//find element
iter = mapS.find("192.168.0.33");
if (iter != mapS.end())
{
cout << "find the elememt" << endl;
cout << "It is:" << iter->second << endl;
}
else
{
cout << "not find the element" << endl;
}
//see element
for (iter = mapS.begin(); iter != mapS.end(); iter++ )
{
cout << "| " << iter->first << " | " << iter->
second << " |" << endl;
}
cout << "-----------------------------" << endl;
map_insert(&mapS, "192.168.30.23", "CT");
cout << "After We Insert One Element:" << endl;
cout << "-----------------------------" << endl;
for (iter = mapS.begin(); iter != mapS.end(); iter++ )
{
cout << "| " << iter->first << " | " << iter->
second << " |" << endl;
}
cout << "-----------------------------" << endl;
//delete element
iter = mapS.find("192.168.200.33");
if (iter != mapS.end())
{
cout << "find the element:" << iter->first << endl;
cout << "delete element:" << iter->first << endl;
cout << "=================================" << endl;
mapS.erase(iter);
}
else
{
cout << "not find the element" << endl;
}
for (iter = mapS.begin(); iter != mapS.end(); iter++ )
{
cout << "| " << iter->first << " | " << iter->
second << " |" << endl;
}
cout << "=================================" << endl;
return 0;
}
/***************
We Have Third Element:
-----------------------------
not find the element
| 192.168.0.128 | uestc |
| 192.168.200.3 | EE |
| 192.168.200.33 | CS |
-----------------------------
After We Insert One Element:
-----------------------------
| 192.168.0.128 | uestc |
| 192.168.200.3 | EE |
| 192.168.200.33 | CS |
| 192.168.30.23 | CT |
-----------------------------
find the element:192.168.200.33
delete element:192.168.200.33
=================================
| 192.168.0.128 | uestc |
| 192.168.200.3 | EE |
| 192.168.30.23 | CT |
=================================
Process returned 0 (0x0) execution time : 1.080 s
Press any key to continue.
*****************/