C++map的基本操作和使用
C++map的基本操作和使用??
1、map簡介
map是一類關(guān)聯(lián)式容器。它的特點是增加和刪除節(jié)點對迭代器的影響很小,除了那個操作節(jié)點,對其他的節(jié)點都沒有什么影響。對于迭代器來說,可以修改實值,而不能修改key。
2、map的功能
自動建立Key - value的對應(yīng)。key 和 value可以是任意你需要的類型。 根據(jù)key值快速查找記錄,查找的復(fù)雜度基本是Log(N),如果有1000個記錄,最多查找10次,1,000,000個記錄,最多查找20次。 快速插入Key - Value 記錄。 快速刪除記錄 根據(jù)Key 修改value記錄。 遍歷所有記錄。 3、使用map
使用map得包含map類所在的頭文件
#include <map> //注意,STL頭文件沒有擴展名.h
map對象是模板類,需要關(guān)鍵字和存儲對象兩個模板參數(shù):
std:map<int, string> personnel;
這樣就定義了一個用int作為索引,并擁有相關(guān)聯(lián)的指向string的指針.
為了使用方便,可以對模板類進行一下類型定義,
typedef map<int, CString> UDT_MAP_INT_CSTRING;
UDT_MAP_INT_CSTRING enumMap;
4、在map中插入元素
改變map中的條目非常簡單,因為map類已經(jīng)對[]操作符進行了重載
enumMap[1] = "One";
enumMap[2] = "Two";
.....
這樣非常直觀,但存在一個性能的問題。插入2時,先在enumMap中查找主鍵為2的項,沒發(fā)現(xiàn),然后將一個新的對象插入enumMap,鍵是2,值是一個空字符串,插入完成后,將字符串賦為"Two"; 該方法會將每個值都賦為缺省值,然后再賦為顯示的值,如果元素是類對象,則開銷比較大。我們可以用以下方法來避免開銷:
enumMap.insert(map<int, CString> :: value_type(2, "Two"))
5、查找并獲取map中的元素
下標(biāo)操作符給出了獲得一個值的最簡單方法:
CString tmp = enumMap[2];
但是,只有當(dāng)map中有這個鍵的實例時才對,否則會自動插入一個實例,值為初始化值。
我們可以使用Find()和Count()方法來發(fā)現(xiàn)一個鍵是否存在。
查找map中是否包含某個關(guān)鍵字條目用find()方法,傳入的參數(shù)是要查找的key,在這里需要提到的是begin()和end()兩個成員,分別代表map對象中第一個條目和最后一個條目,這兩個數(shù)據(jù)的類型是iterator.
int nFindKey = 2; //要查找的Key
//定義一個條目變量(實際是指針)
UDT_MAP_INT_CSTRING::iterator it= enumMap.find(nFindKey);
if(it == enumMap.end()) {
//沒找到
}
else {
//找到
}
通過map對象的方法獲取的iterator數(shù)據(jù)類型是一個std::pair對象,包括兩個數(shù)據(jù) iterator->first 和 iterator->second 分別代表關(guān)鍵字和存儲的數(shù)據(jù)
6、從map中刪除元素
移除某個map中某個條目用erase()
該成員方法的定義如下
iterator erase(iterator it); //通過一個條目對象刪除 iterator erase(iterator first, iterator last); //刪除一個范圍 size_type erase(const Key& key); //通過關(guān)鍵字刪除 clear()就相當(dāng)于 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;}
總結(jié)
以上是生活随笔為你收集整理的C++map的基本操作和使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows 命名管道 + 异步I/O
- 下一篇: C++后台服务程序开发模式