C++中的STL中map用法詳解(零基礎(chǔ)入門)
map 在編程中是經(jīng)常使用的一個(gè)容器,本文來講解一下 STL 中的 map,趕緊來看下吧!
一、什么是 map ?
map 是具有唯一鍵值對(duì)的容器,通常使用紅黑樹實(shí)現(xiàn)。
map 中的鍵值對(duì)是 key value 的形式,比如:每個(gè)身份證號(hào)對(duì)應(yīng)一個(gè)人名(反過來不成立哦!),其中,身份證號(hào)就是 key,人名便是 value,是單項(xiàng)的關(guān)系,可以與 hash 作類比。
二、map的定義
2.1 頭文件
使用 map 需要引入頭文件,如下所示:
#include <map>
2.2 定義
定義形式如下所示:
map<key_type, value_type>變量名
注意:如果沒有 using namespace std, map需要寫成 std:map。
來看一個(gè)簡(jiǎn)單的例子:
#include <iostream>
#include <map> // 頭文件
#include <string>
using namespace std;
int main() {
map<int, string>node; // 定義變量
node[123456] = "張三";
cout<<"身份證號(hào)123456的人叫"<<node[123456]<<endl;
}
輸出為:
身份證號(hào)123456的人叫張三
在上例中,定義了一個(gè)key 為 int ,value 為 string 的 map 容器 node。
2.3 方法
map 最常見的方法如下所示:
//常用 size() // 計(jì)算元素個(gè)數(shù) empty() // 判斷是否為空,空返回 true clear() // 清空容器 erase() // 刪除元素 find() // 查找元素 insert() // 插入元素 count() // 計(jì)算指定元素出現(xiàn)的次數(shù) begin() // 返回迭代器頭部 end() // 返回迭代器尾部 //非常用 swap() // 交換兩個(gè)map容器,類型需要相同 max_size() // 容納的最大元素個(gè)數(shù) rbegin() // 指向map尾部的逆向迭代器 rend() // 指向map頭部的逆向迭代器 lower_bound() // 返回鍵值大于等于指定元素的第一個(gè)位置 upper_bound() // 返回鍵值大于指定元素的第一個(gè)位置 equal_range() // 返回等于指定元素的區(qū)間
三、實(shí)例講解
3.1 增加數(shù)據(jù)
方法1:以數(shù)組下標(biāo)的形式直接增加,即:變量名[key] = value 的形式。
#include <iostream>
#include <map> // 頭文件
#include <string>
using namespace std;
int main() {
map<int, string>node; // 定義變量
node[123456] = "張三";
node[123457] = "李四";
node[123458] = "王五";
cout<<"身份證號(hào)123456的人叫"<<node[123456]<<endl;
cout<<"身份證號(hào)123457的人叫"<<node[123457]<<endl;
cout<<"身份證號(hào)123458的人叫"<<node[123458]<<endl;
}
輸出為:
身份證號(hào)123456的人叫張三
身份證號(hào)123457的人叫李四
身份證號(hào)123458的人叫王五
方法2:直接插入鍵值對(duì)。
#include <iostream>
#include <map> // 頭文件
#include <string>
using namespace std;
int main() {
map<int, string>node; // 定義變量
node.insert(pair<int, string>(123456, "張三"));
node.insert(pair<int, string>(123457, "張三"));
node.insert(pair<int, string>(123458, "李四"));
cout<<"身份證號(hào)123456的人叫"<<node[123456]<<endl;
cout<<"身份證號(hào)123457的人叫"<<node[123457]<<endl;
cout<<"身份證號(hào)123458的人叫"<<node[123458]<<endl;
}
輸出為:
身份證號(hào)123456的人叫張三
身份證號(hào)123457的人叫張三
身份證號(hào)123458的人叫李四
其中,pair 定義了一個(gè)鍵值對(duì),對(duì)應(yīng) map 的 key 和 value。
3.2 刪除數(shù)據(jù)
刪除數(shù)據(jù)使用到 map 的 erase 和 clear方法,來看一下例子:
#include <iostream>
#include <map> // 頭文件
#include <string>
using namespace std;
int main() {
map<int, string>node; // 定義變量
node[123456] = "張三";
node[123457] = "李四";
node[123458] = "王五";
cout<<"size = "<<node.size()<<endl;
//1. 使用 key 刪除
node.erase(123456); // 刪除 key = 123456 的節(jié)點(diǎn)
cout<<"size = "<<node.size()<<endl;
//2. 使用迭代器刪除
map<int,string>::iterator iter = node.find(123457);
node.erase(iter);
cout<<"size = "<<node.size()<<endl;
//3. 清空整個(gè)容器
node.clear();
cout<<"size = "<<node.size()<<endl;
}
輸出為:
size = 3
size = 2
size = 1
size = 0
其中,clear 方法表示清空容器,size 方法表示獲取容器大小。
3.3 修改數(shù)據(jù)
修改數(shù)據(jù)僅能修改 value 的值,key 是不能修改的,可以通過增加和刪除來實(shí)現(xiàn)修改 key。
#include <iostream>
#include <map> // 頭文件
#include <string>
using namespace std;
int main() {
map<int, string>node; // 定義變量
node[123456] = "張三";
cout<<"身份證號(hào)123456的人叫"<<node[123456]<<endl;
node[123456] = "李四";
cout<<"身份證號(hào)123456的人叫"<<node[123456]<<endl;
}
輸出為:
身份證號(hào)123456的人叫張三
身份證號(hào)123456的人叫李四
3.4 查找數(shù)據(jù)
查找數(shù)據(jù)通過 find 函數(shù)來實(shí)現(xiàn),如下所示:
#include <iostream>
#include <map> // 頭文件
#include <string>
using namespace std;
int main() {
map<int, string>node; // 定義變量
node[123456] = "張三";
node[123457] = "李四";
node[123458] = "王五";
map<int, string>::iterator iter = node.find(123456);
if(iter != node.end()) {
cout<<"身份證號(hào)123456的人叫"<<iter->second<<endl;
}
}
輸出為:
身份證號(hào)123456的人叫張三
find 方法返回的是 map 的迭代器。
3.5 遍歷元素
遍歷元素使用迭代器的方式,如下所示:
#include <iostream>
#include <map> // 頭文件
#include <string>
using namespace std;
int main() {
map<int, string>node; // 定義變量
node[123456] = "張三";
node[123457] = "李四";
node[123458] = "王五";
map<int, string>::iterator iter; //定義迭代器 iter
for(iter = node.begin(); iter != node.end(); ++iter) {
cout<<"身份證號(hào)"<<iter->first<<"的人叫"<<iter->second<<endl;
}
}
輸出為:
身份證號(hào)123456的人叫張三
身份證號(hào)123457的人叫李四
身份證號(hào)123458的人叫王五
其中,使用迭代器 iter 遍歷容器,可以將迭代器理解為一個(gè)存儲(chǔ)了 key 和 value 的一個(gè)結(jié)構(gòu),first 對(duì)應(yīng) key,second 對(duì)應(yīng) value。
3.6 其它方法
(1)swap 函數(shù)
交換兩個(gè) map 容器的內(nèi)容,map 容器的類型必須相同,例如:
#include <iostream>
#include <map> // 頭文件
#include <string>
using namespace std;
int main() {
map<int, string>node1; // 定義變量
map<int, string>node2;
node1[11] = "張三";
node1[12] = "李四";
node2[21] = "王五";
node2[22] = "趙六";
node2[23] = "孫七";
node1.swap(node2);
map<int, string>::iterator iter;
cout<<"node1 :"<<endl;
for(iter = node1.begin(); iter != node1.end(); ++iter) {
cout<<"key = "<<iter->first<<" value = "<<iter->second<<endl;
}
cout<<"node2 :"<<endl;
for(iter = node2.begin(); iter != node2.end(); ++iter) {
cout<<"key = "<<iter->first<<" value = "<<iter->second<<endl;
}
}
輸出為:
node1 :
key = 21 value = 王五
key = 22 value = 趙六
key = 23 value = 孫七
node2 :
key = 11 value = 張三
key = 12 value = 李四
(2)max_size
返回當(dāng)前容器的可以容納的最大元素個(gè)數(shù),來看一個(gè)例子。
#include <iostream>
#include <map> // 頭文件
#include <string>
using namespace std;
int main() {
map<int, string>node; // 定義變量
cout<<"max_size = "<<node.max_size()<<endl;
node[11] = "張三";
cout<<"max_size = "<<node.max_size()<<endl;
node[12] = "李四";
cout<<"max_size = "<<node.max_size()<<endl;
node[13] = "王五";
cout<<"max_size = "<<node.max_size()<<endl;
}
輸出為:
max_size = 128102389400760775
max_size = 128102389400760775
max_size = 128102389400760775
max_size = 128102389400760775
(3)rbegin 和 rend
rbegin 和 rend 為反向迭代器,即:rbegin 指向最后一個(gè)元素,rend 指向第一個(gè)元素的前一個(gè)位置,來看一個(gè)例子。
#include <iostream>
#include <map> // 頭文件
#include <string>
using namespace std;
int main() {
map<int, string>node; // 定義變量
node[11] = "張三";
node[12] = "李四";
node[13] = "王五";
map<int, string>::reverse_iterator iter;
for(iter = node.rbegin(); iter != node.rend(); ++iter) {
cout<<"key = "<<iter->first<<" value = "<<iter->second<<endl;
}
}
輸出為:
key = 13 value = 王五
key = 12 value = 李四
key = 11 value = 張三
注意:迭代器需要使用反向迭代器。
(4)lower_bound 和 upper_bound
#include <iostream>
#include <map> // 頭文件
#include <string>
using namespace std;
int main() {
map<int, string>node; // 定義變量
node[20] = "張三";
node[15] = "李四";
node[12] = "王五";
map<int, string>::iterator iter = node.lower_bound(14);
cout<<"key = "<<iter->first<<" value = "<<iter->second<<endl;
iter = node.upper_bound(12);
cout<<"key = "<<iter->first<<" value = "<<iter->second<<endl;
}
輸出結(jié)果為:
key = 15 value = 李四
key = 15 value = 李四
(5)equal_range
#include <iostream>
#include <map> // 頭文件
#include <string>
using namespace std;
int main() {
map<int, string>node; // 定義變量
node[12] = "張三";
node[15] = "李四";
node[20] = "王五";
pair<map<int, string>::iterator, map<int, string>::iterator> p = node.equal_range(15);
cout<<"key1 = "<<p.first->first<<" value1 = "<<p.first->second<<endl;
cout<<"key2 = "<<p.second->first<<" value2 = "<<p.second->second<<endl;
}
輸出為:
key1 = 15 value1 = 李四
key2 = 20 value2 = 王五
四、總結(jié)
編程中經(jīng)常使用到 key / value 的形式表示數(shù)據(jù)之間的關(guān)系,故 map 是 STL 中經(jīng)常使用的一個(gè)容器,需要記住 map 的常用方法。
到此這篇關(guān)于C++中的STL中map用法詳解(零基礎(chǔ)入門)的文章就介紹到這了,更多相關(guān)C++ STL map用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VC中CWinThread類以及和createthread API的區(qū)別分析
這篇文章主要介紹了VC中CWinThread類以及和createthread API的區(qū)別分析,較為詳細(xì)的講述了CWinThread類的原理,并以實(shí)例形式對(duì)AfxBeginThread函數(shù)的內(nèi)部實(shí)現(xiàn)進(jìn)行了解釋說明,需要的朋友可以參考下2014-10-10
淺談c++性能測(cè)試工具google benchmark
本文將會(huì)介紹如何使用模板以及參數(shù)生成器來批量生成測(cè)試用例,簡(jiǎn)化繁瑣的性能測(cè)試代碼2021-06-06
C++ 虛函數(shù)與純虛函數(shù)的使用與區(qū)別
本文主要介紹了C++ 虛函數(shù)與純虛函數(shù)的使用與區(qū)別,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
C++中4種強(qiáng)制類型轉(zhuǎn)換的區(qū)別詳析
這篇文章主要給大家介紹了關(guān)于C++中4種強(qiáng)制類型轉(zhuǎn)換區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
C/C++根據(jù)年月日計(jì)算星期幾(蔡勒公式篇)
這篇文章主要給大家介紹了關(guān)于C/C++根據(jù)年月日計(jì)算星期幾(蔡勒公式篇)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
C語(yǔ)言代碼實(shí)現(xiàn)簡(jiǎn)單的掃雷小游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言代碼實(shí)現(xiàn)簡(jiǎn)單的掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03

