日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++map的基本操作和使用

發布時間:2025/3/21 c/c++ 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++map的基本操作和使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C++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"))

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++ STLmap的使用

以下是對C++中STLmap的插入,查找,遍歷及刪除的例子:

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

總結

以上是生活随笔為你收集整理的C++map的基本操作和使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。