Map是c++的一個標準容器,她提供了很好一對一的關係,在一些程序中建立一個map可以起到事半功倍的效果,總結了一些map基本簡單實用的操作!
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"))
enumMap.insert(pair<int,string>(102,"aclive"));
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());
7.map中的swap用法
Map中德swap不是一個容器中的元素交換,而是兩個容器的交換;
8.map中的sort問題
Map中的元素是自動按Key升序排序,所以不能對map用sort函數;
9. map的基本操作函數:
C++ Maps是一種關聯式容器,包含“關鍵字/值”對
begin() 返回指向map頭部的迭代器
clear() 刪除所有元素
count() 返回指定元素出現的次數
empty() 如果map爲空則返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊條目的迭代器對
erase() 刪除一個元素
find() 查找一個元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比較元素key的函數
lower_bound() 返回鍵值>=給定元素的第一個位置
max_size() 返回可以容納的最大元素個數
rbegin() 返回一個指向map尾部的逆向迭代器
rend() 返回一個指向map頭部的逆向迭代器
size() 返回map中元素的個數
swap() 交換兩個map
upper_bound() 返回鍵值>給定元素的第一個位置
value_comp() 返回比較元素value的函數
以下是map容器的應用實例:
- #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", "xiong");
- map_insert(&mapS, "192.168.200.3", "feng");
- map_insert(&mapS, "192.168.200.33", "xiongfeng");
- 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", "xf");
- 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;
- }