c++map容器介紹

 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,stringpersonnel;

這樣就定義了一個用int作爲索引,並擁有相關聯的指向string的指針.

爲了使用方便,可以對模板類進行一下類型定義,

typedef map<int,CStringUDT_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 nFindKey2;//要查找的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容器的應用實例:

[cpp] view plain copy
  1. #include <map>  
  2. #include <string>   
  3. #include <iostream>   
  4. using namespace std;  
  5.   
  6. void map_insert(map < string, string > *mapStudent, string index, string x)  
  7. {   
  8. mapStudent->insert(map < string, string >::value_type(index, x));  
  9. }  
  10.   
  11. int main(int argc, char **argv)   
  12. {   
  13. char tmp[32] = "";   
  14. map < string, string > mapS;  
  15.   
  16. //insert element   
  17. map_insert(&mapS, "192.168.0.128""xiong");  
  18. map_insert(&mapS, "192.168.200.3""feng");  
  19. map_insert(&mapS, "192.168.200.33""xiongfeng");  
  20.   
  21. map < string, string >::iterator iter;  
  22.   
  23. cout << "We Have Third Element:" << endl;   
  24. cout << "-----------------------------" << endl;  
  25.   
  26. //find element   
  27. iter = mapS.find("192.168.0.33");   
  28. if (iter != mapS.end()) {   
  29. cout << "find the elememt" << endl;   
  30. cout << "It is:" << iter->second << endl;   
  31. else {   
  32. cout << "not find the element" << endl;   
  33. }  
  34.   
  35. //see element   
  36. for (iter = mapS.begin(); iter != mapS.end(); iter ) {  
  37.   
  38. cout << "| " << iter->first << " | " << iter->   
  39. second << " |" << endl;  
  40.   
  41. }   
  42. cout << "-----------------------------" << endl;  
  43.   
  44. map_insert(&mapS, "192.168.30.23""xf");  
  45.   
  46. cout << "After We Insert One Element:" << endl;   
  47. cout << "-----------------------------" << endl;   
  48. for (iter = mapS.begin(); iter != mapS.end(); iter ) {  
  49.   
  50. cout << "| " << iter->first << " | " << iter->   
  51. second << " |" << endl;   
  52. }  
  53.   
  54. cout << "-----------------------------" << endl;  
  55.   
  56. //delete element   
  57. iter = mapS.find("192.168.200.33");   
  58. if (iter != mapS.end()) {   
  59. cout << "find the element:" << iter->first << endl;   
  60. cout << "delete element:" << iter->first << endl;   
  61. cout << "=================================" << endl;   
  62. mapS.erase(iter);   
  63. else {   
  64. cout << "not find the element" << endl;   
  65. }   
  66. for (iter = mapS.begin(); iter != mapS.end(); iter ) {  
  67.   
  68. cout << "| " << iter->first << " | " << iter->   
  69. second << " |" << endl;  
  70.   
  71. }   
  72. cout << "=================================" << endl;  
  73.   
  74. return 0;   
  75. }   


 

 

 

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章