C++ std:map的使用方法
std::map 是 C++ 標(biāo)準(zhǔn)庫(kù)中一個(gè)強(qiáng)大而高效的關(guān)聯(lián)容器,提供鍵-值對(duì)存儲(chǔ)、自動(dòng)排序以及高效查找的功能。在實(shí)際開發(fā)中,std::map 在需要有序存儲(chǔ)和快速檢索的場(chǎng)景中非常實(shí)用。本篇文章將詳細(xì)解析 std::map 的功能和用法,并配有代碼示例,幫助你更好地掌握它。
1. 什么是 std::map
std::map 是 C++ STL(Standard Template Library中的一種關(guān)聯(lián)容器,提供了鍵值對(duì)的存儲(chǔ)形式,其中每個(gè)鍵是唯一的并且按順序排列。具體來說:
- 鍵(Key):每個(gè)鍵是唯一的,并用于查找特定的值。
- 值(Value):鍵所對(duì)應(yīng)的內(nèi)容,即要存儲(chǔ)的數(shù)據(jù)
為什么使用 std::map
- 快速查找:std::map 使用平衡二叉樹(通常為紅黑樹)實(shí)現(xiàn),具有O(logn) 的查找、插入和刪除效率。
- 自動(dòng)排序:std::map 會(huì)自動(dòng)按鍵升序排列,省去了手動(dòng)排序的麻煩。
- 鍵唯一性:同一 map 中不允許有重復(fù)的鍵,這使它適合存儲(chǔ)唯一的鍵-值對(duì)。
2. std::map 的基本用法
2.1 定義和初始化 std::map
std::map 可以通過多種方式進(jìn)行定義和初始化。常見的定義方法如下:
#include <iostream> #include <map> using namespace std; int main() { // 定義一個(gè)鍵為 int,值為 string 的 map map<int, string> studentMap; // 使用初始化列表直接初始化 map map<int, string> employeeMap = { {1, "Alice"}, {2, "Bob"}, {3, "Charlie"} }; return 0; }
2.2 插入元素
std::map 提供了兩種插入方式:使用 insert 方法和 [ ] 運(yùn)算符。
map<int, string> studentMap; // 方法 1:使用 insert 插入鍵值對(duì) studentMap.insert({1, "Alice"}); // 方法 2:使用 [] 運(yùn)算符直接插入 studentMap[2] = "Bob"; // 輸出所有鍵值對(duì) for (const auto &entry : studentMap) { cout << entry.first << ": " << entry.second << endl; }
- 使用 [ ] 操作符的插入方式更方便,而且如果鍵已存在,則會(huì)更新對(duì)應(yīng)的值。
2.3 查找元素
使用 find 函數(shù)可以查找特定的鍵。如果找到則返回一個(gè)指向該元素的迭代器,否則返回 map::end() 迭代器。
auto 是 C++11 引入的一種自動(dòng)類型推導(dǎo)關(guān)鍵字,用于讓編譯器自動(dòng)推導(dǎo)變量的類型。
auto it = studentMap.find(1); // 查找鍵為 1 的元素 if (it != studentMap.end()) { cout << "Found: " << it->second << endl; } else { cout << "Not found!" << endl; }
2.4 刪除元素
std::map 提供了多種刪除方式:按鍵刪除、按迭代器刪除、刪除特定范圍的元素。
// 按鍵刪除 studentMap.erase(1); // 按迭代器刪除 auto it = studentMap.find(2); if (it != studentMap.end()) { studentMap.erase(it); }
3. std::map 常用函數(shù)詳解
3.1 大小與檢查是否為空:size 和 empty
- size:返回 map 中的鍵值對(duì)數(shù)量。
- empty:判斷 map 是否為空。
cout << "Size: " << studentMap.size() << endl; if (studentMap.empty()) { cout << "Map is empty" << endl; }
3.2 遍歷 std::map
遍歷 map 的常見方式是使用范圍基于 for 循環(huán)或迭代器。
// 使用范圍基于 for 循環(huán) for (const auto &entry : studentMap) { cout << entry.first << ": " << entry.second << endl; } // 使用迭代器 for (auto it = studentMap.begin(); it != studentMap.end(); ++it) { cout << it->first << ": " << it->second << endl; }
3.3 統(tǒng)計(jì)鍵的數(shù)量:count
count 函數(shù)返回特定鍵的數(shù)量,對(duì)于 std::map 來說,鍵是唯一的,因此結(jié)果要么是 0,要么是 1。
if (studentMap.count(2) > 0) { cout << "Key 2 exists" << endl; }
3.4 獲取范圍:lower_bound 和 upper_bound
- lower_bound:返回第一個(gè)不小于指定鍵的迭代器。
- upper_bound:返回第一個(gè)大于指定鍵的迭代器。
map<int, string> employeeMap = { {1, "Alice"}, {3, "Bob"}, {5, "Charlie"} }; // 獲取范圍 auto lb = employeeMap.lower_bound(3); // 指向鍵 3 的位置 auto ub = employeeMap.upper_bound(3); // 指向鍵 5 的位置 if (lb != employeeMap.end()) { cout << "Lower bound: " << lb->first << endl; } if (ub != employeeMap.end()) { cout << "Upper bound: " << ub->first << endl; }
3.5 清空 map
clear 函數(shù)清空 map 中的所有元素。
studentMap.clear(); if (studentMap.empty()) { cout << "Map is now empty" << endl; }
3.6 排序
std::map 的一個(gè)重要特性是自動(dòng)排序,即在插入新元素后,map 會(huì)按鍵的升序排列。我們無需手動(dòng)排序或管理順序,std::map 會(huì)始終保持內(nèi)部的有序性。下面我們來看看如何體現(xiàn)這一特性,以及實(shí)際示例展示 std::map 的自動(dòng)排序。
3.6.1 自動(dòng)排序示例
當(dāng)我們往 map 中插入元素時(shí),map 會(huì)自動(dòng)按鍵升序排列存儲(chǔ)。即使插入順序是隨機(jī)的,遍歷時(shí)鍵值對(duì)的輸出順序依舊是有序的。
#include <iostream> #include <map> using namespace std; int main() { map<int, string> studentMap; // 按隨機(jī)順序插入元素 studentMap[3] = "Charlie"; studentMap[1] = "Alice"; studentMap[4] = "Diana"; studentMap[2] = "Bob"; // 遍歷并打印元素,觀察輸出順序 cout << "學(xué)生名單 (按學(xué)號(hào)升序自動(dòng)排序):" << endl; for (const auto &entry : studentMap) { cout << "學(xué)號(hào): " << entry.first << ", 姓名: " << entry.second << endl; } return 0; }
3.6.2 自定義排序(高級(jí)用法)
除了默認(rèn)的升序排列,std::map 還允許自定義排序。可以在定義 map 時(shí),通過傳入自定義比較函數(shù),實(shí)現(xiàn)按降序或其他規(guī)則排序。
#include <iostream> #include <map> #include <string> using namespace std; // 自定義比較函數(shù),按降序排序 struct DescendingOrder { bool operator()(const int &a, const int &b) const { return a > b; } }; int main() { map<int, string, DescendingOrder> studentMap; // 插入元素 studentMap[3] = "Charlie"; studentMap[1] = "Alice"; studentMap[4] = "Diana"; studentMap[2] = "Bob"; // 輸出 map 的內(nèi)容 cout << "學(xué)生名單 (按學(xué)號(hào)降序排序):" << endl; for (const auto &entry : studentMap) { cout << "學(xué)號(hào): " << entry.first << ", 姓名: " << entry.second << endl; } return 0; }
在這里,我們定義了一個(gè)比較函數(shù) DescendingOrder,并將其作為 map 的第三個(gè)模板參數(shù)傳入,使得 map 按鍵降序排列。這樣,我們就可以輕松調(diào)整 map 的排序方式。
4. std::map 使用注意事項(xiàng)
性能考慮std::map 使用平衡二叉樹實(shí)現(xiàn),因此查找、插入和刪除的時(shí)間復(fù)雜度為 O(logn)。如果你的數(shù)據(jù)需要頻繁插入或刪除,并且對(duì)鍵的順序不敏感,可以考慮 unordered_map,其時(shí)間復(fù)雜度為 O(1)。
默認(rèn)構(gòu)造鍵值對(duì)當(dāng)使用 [] 運(yùn)算符查找鍵時(shí),如果鍵不存在,std::map 會(huì)自動(dòng)插入該鍵并設(shè)置為默認(rèn)值。這在無意中訪問不存在的鍵時(shí)可能導(dǎo)致錯(cuò)誤。
map<int, int> numbers; numbers[5] += 1; // 如果鍵 5 不存在,則會(huì)創(chuàng)建并初始化為 0,然后加 1
5. 經(jīng)典示例:統(tǒng)計(jì)單詞頻率
下面是一個(gè)使用 std::map 統(tǒng)計(jì)文本中單詞出現(xiàn)頻率的示例:
#include <iostream> #include <map> #include <string> #include <sstream> using namespace std; int main() { map<string, int> wordCount; string text = "this is a sample text with sample words and sample frequencies"; stringstream ss(text); // 將字符串 text 放入 stringstream 中 string word; while (ss >> word) { // 從 ss 中逐個(gè)提取單詞并存儲(chǔ)到 word wordCount[word]++; // 統(tǒng)計(jì)每個(gè)單詞的出現(xiàn)次數(shù) } for (const auto &entry : wordCount) { cout << entry.first << ": " << entry.second << endl; } return 0; }
到此這篇關(guān)于C++ std:map的使用方法的文章就介紹到這了,更多相關(guān)C++ std:map使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c語言大小端(數(shù)據(jù)在內(nèi)存中的存儲(chǔ))
大小端是內(nèi)存存儲(chǔ)字節(jié)的兩種方式,一個(gè)是大端存儲(chǔ),一個(gè)是小端存儲(chǔ),本文主要介紹了c語言大小端,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09使用C語言中的time函數(shù)獲取系統(tǒng)時(shí)間
在C語言中可以使用time函數(shù)來獲取系統(tǒng)時(shí)間,以下對(duì)time函數(shù)進(jìn)行了介紹,需要的朋友可以過來參考下2013-07-07C++利用模板實(shí)現(xiàn)消息訂閱和分發(fā)功能
C++語言支持重載,模板,虛函數(shù)等特性,為編寫高性能可擴(kuò)展的程序提供了利器。本文就講利用模板實(shí)現(xiàn)消息訂閱和分發(fā)功能,感興趣的小伙伴可以了解一下2022-12-12C++實(shí)現(xiàn)俄羅斯方塊(windows API)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)俄羅斯方塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06基于c++的中國(guó)象棋游戲設(shè)計(jì)與實(shí)現(xiàn)
這篇文章主要介紹了基于c++的中國(guó)象棋游戲設(shè)計(jì)與實(shí)現(xiàn),主要操作是possibleMove(int?x,?int?y),通過整個(gè)棋盤每個(gè)位置上的信息、中國(guó)象棋的規(guī)則來獲得位置(x,?y)這個(gè)棋子可以移動(dòng)到的位置,需要的朋友可以參考一下2022-02-02