C++ STL map的使用

來源:互聯網
上載者:User

1、map簡介

map是一類關聯式容器。它的特點是增加和刪除節點對迭代器的影響很小,除了那個動作節點,對其他的節點都沒有什麼影響。對於迭代器來說,可以修改實值,而不能修改key。

2、map的功能

  1. 自動建立Key - value的對應。key 和 value可以是任意你需要的類型。
  2. 根據key值快速尋找記錄,尋找的複雜度基本是Log(N),如果有1000個記錄,最多尋找10次,1,000,000個記錄,最多尋找20次。
  3. 快速插入Key - Value 記錄。
  4. 快速刪除記錄
  5. 根據Key 修改value記錄。
  6. 遍曆所有記錄。

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,值是一個Null 字元串,插入完成後,將字串賦為"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()

該成員方法的定義如下

  1. iterator erase(iterator it); //通過一個條目對象刪除
  2. iterator erase(iterator first, iterator last); //刪除一個範圍
  3. size_type erase(const Key& key); //通過關鍵字刪除

clear()就相當於 enumMap.erase(enumMap.begin(), enumMap.end());

C++ STL map的使用

以下是對C++中STL 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;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.