C++ std:map的使用方法
std::map 是 C++ 標(biāo)準(zhǔn)庫中一個強大而高效的關(guān)聯(lián)容器,提供鍵-值對存儲、自動排序以及高效查找的功能。在實際開發(fā)中,std::map 在需要有序存儲和快速檢索的場景中非常實用。本篇文章將詳細(xì)解析 std::map 的功能和用法,并配有代碼示例,幫助你更好地掌握它。
1. 什么是 std::map
std::map 是 C++ STL(Standard Template Library中的一種關(guān)聯(lián)容器,提供了鍵值對的存儲形式,其中每個鍵是唯一的并且按順序排列。具體來說:
- 鍵(Key):每個鍵是唯一的,并用于查找特定的值。
- 值(Value):鍵所對應(yīng)的內(nèi)容,即要存儲的數(shù)據(jù)
為什么使用 std::map
- 快速查找:std::map 使用平衡二叉樹(通常為紅黑樹)實現(xiàn),具有O(logn) 的查找、插入和刪除效率。
- 自動排序:std::map 會自動按鍵升序排列,省去了手動排序的麻煩。
- 鍵唯一性:同一 map 中不允許有重復(fù)的鍵,這使它適合存儲唯一的鍵-值對。
2. std::map 的基本用法
2.1 定義和初始化 std::map
std::map 可以通過多種方式進行定義和初始化。常見的定義方法如下:
#include <iostream>
#include <map>
using namespace std;
int main() {
// 定義一個鍵為 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 方法和 [ ] 運算符。
map<int, string> studentMap;
// 方法 1:使用 insert 插入鍵值對
studentMap.insert({1, "Alice"});
// 方法 2:使用 [] 運算符直接插入
studentMap[2] = "Bob";
// 輸出所有鍵值對
for (const auto &entry : studentMap) {
cout << entry.first << ": " << entry.second << endl;
}
- 使用 [ ] 操作符的插入方式更方便,而且如果鍵已存在,則會更新對應(yīng)的值。
2.3 查找元素
使用 find 函數(shù)可以查找特定的鍵。如果找到則返回一個指向該元素的迭代器,否則返回 map::end() 迭代器。
auto 是 C++11 引入的一種自動類型推導(dǎo)關(guān)鍵字,用于讓編譯器自動推導(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 中的鍵值對數(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)計鍵的數(shù)量:count
count 函數(shù)返回特定鍵的數(shù)量,對于 std::map 來說,鍵是唯一的,因此結(jié)果要么是 0,要么是 1。
if (studentMap.count(2) > 0) {
cout << "Key 2 exists" << endl;
}
3.4 獲取范圍:lower_bound 和 upper_bound
- lower_bound:返回第一個不小于指定鍵的迭代器。
- upper_bound:返回第一個大于指定鍵的迭代器。
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 的一個重要特性是自動排序,即在插入新元素后,map 會按鍵的升序排列。我們無需手動排序或管理順序,std::map 會始終保持內(nèi)部的有序性。下面我們來看看如何體現(xiàn)這一特性,以及實際示例展示 std::map 的自動排序。
3.6.1 自動排序示例
當(dāng)我們往 map 中插入元素時,map 會自動按鍵升序排列存儲。即使插入順序是隨機的,遍歷時鍵值對的輸出順序依舊是有序的。
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> studentMap;
// 按隨機順序插入元素
studentMap[3] = "Charlie";
studentMap[1] = "Alice";
studentMap[4] = "Diana";
studentMap[2] = "Bob";
// 遍歷并打印元素,觀察輸出順序
cout << "學(xué)生名單 (按學(xué)號升序自動排序):" << endl;
for (const auto &entry : studentMap) {
cout << "學(xué)號: " << entry.first << ", 姓名: " << entry.second << endl;
}
return 0;
}
3.6.2 自定義排序(高級用法)
除了默認(rèn)的升序排列,std::map 還允許自定義排序。可以在定義 map 時,通過傳入自定義比較函數(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é)號降序排序):" << endl;
for (const auto &entry : studentMap) {
cout << "學(xué)號: " << entry.first << ", 姓名: " << entry.second << endl;
}
return 0;
}
在這里,我們定義了一個比較函數(shù) DescendingOrder,并將其作為 map 的第三個模板參數(shù)傳入,使得 map 按鍵降序排列。這樣,我們就可以輕松調(diào)整 map 的排序方式。
4. std::map 使用注意事項
性能考慮std::map 使用平衡二叉樹實現(xiàn),因此查找、插入和刪除的時間復(fù)雜度為 O(logn)。如果你的數(shù)據(jù)需要頻繁插入或刪除,并且對鍵的順序不敏感,可以考慮 unordered_map,其時間復(fù)雜度為 O(1)。
默認(rèn)構(gòu)造鍵值對當(dāng)使用 [] 運算符查找鍵時,如果鍵不存在,std::map 會自動插入該鍵并設(shè)置為默認(rèn)值。這在無意中訪問不存在的鍵時可能導(dǎo)致錯誤。
map<int, int> numbers; numbers[5] += 1; // 如果鍵 5 不存在,則會創(chuàng)建并初始化為 0,然后加 1
5. 經(jīng)典示例:統(tǒng)計單詞頻率
下面是一個使用 std::map 統(tǒng)計文本中單詞出現(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 中逐個提取單詞并存儲到 word
wordCount[word]++; // 統(tǒng)計每個單詞的出現(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c語言大小端(數(shù)據(jù)在內(nèi)存中的存儲)
大小端是內(nèi)存存儲字節(jié)的兩種方式,一個是大端存儲,一個是小端存儲,本文主要介紹了c語言大小端,具有一定的參考價值,感興趣的可以了解一下2023-09-09
使用C語言中的time函數(shù)獲取系統(tǒng)時間
在C語言中可以使用time函數(shù)來獲取系統(tǒng)時間,以下對time函數(shù)進行了介紹,需要的朋友可以過來參考下2013-07-07

