std::map
定義於標頭檔案 <map> |
||
template< class Key, |
(1) | |
namespace pmr { template< |
(2) | (C++17 起) |
std::map
是一個有序的關聯容器,包含具有唯一鍵的鍵值對。鍵透過比較函式 Compare
進行排序。搜尋、刪除和插入操作具有對數複雜度。Map 通常實現為紅黑樹。
std::map
的迭代器按鍵的升序迭代,其中升序由用於構造的比較定義。也就是說,給定
- m,一個
std::map
- it_l 和 it_r,指向 m 的可解引用迭代器,且 it_l < it_r。
m.value_comp()(*it_l, *it_r) == true(如果使用預設比較,則從最小到最大)。
在標準庫使用 Compare 要求的所有地方,唯一性都透過等價關係確定。不精確地說,如果兩個物件 a 和 b 都不比對方小,則它們被認為是等價的(不唯一):!comp(a, b) && !comp(b, a)。
std::map
滿足 Container、AllocatorAwareContainer、AssociativeContainer 和 ReversibleContainer 的要求。
|
(C++26 起) |
目錄 |
[編輯] 模板引數
本節不完整 理由:新增模板引數的描述。 |
[編輯] 成員型別
型別 | 定義 | ||||
key_type
|
Key | ||||
mapped_type
|
T | ||||
value_type
|
std::pair<const Key, T> | ||||
size_type
|
無符號整數型別(通常是 std::size_t) | ||||
difference_type
|
有符號整數型別(通常是 std::ptrdiff_t) | ||||
key_compare
|
Compare | ||||
allocator_type
|
Allocator | ||||
reference
|
value_type& | ||||
const_reference
|
const value_type& | ||||
pointer
|
| ||||
const_pointer
|
| ||||
iterator
|
LegacyBidirectionalIterator 和 ConstexprIterator(C++26起) 到 value_type | ||||
const_iterator
|
LegacyBidirectionalIterator 和 ConstexprIterator(C++26起) 到 const value_type | ||||
reverse_iterator
|
std::reverse_iterator<iterator> | ||||
const_reverse_iterator
|
std::reverse_iterator<const_iterator> | ||||
node_type (C++17 起) |
表示容器節點的節點控制代碼特化 | ||||
insert_return_type (C++17起) |
描述插入 node_type 結果的型別,一個特化template<class Iter, class NodeType> |
[編輯] 成員類
比較 value_type 型別的物件(類) |
[編輯] 成員函式
構造 map (公共成員函式) | |
銷燬 map (公共成員函式) | |
將值賦給容器 (公共成員函式) | |
返回關聯的分配器 (公共成員函式) | |
元素訪問 | |
訪問指定的元素,帶邊界檢查 (公共成員函式) | |
訪問或插入指定元素 (公共成員函式) | |
迭代器 | |
(C++11起) |
返回指向起始的迭代器 (公共成員函式) |
(C++11起) |
返回指向末尾的迭代器 (公共成員函式) |
(C++11起) |
返回指向起始的逆向迭代器 (公共成員函式) |
(C++11起) |
返回指向末尾的逆向迭代器 (公共成員函式) |
容量 | |
檢查容器是否為空 (公共成員函式) | |
返回元素數量 (公共成員函式) | |
返回元素的最大可能數量 (公共成員函式) | |
修改器 | |
清除內容 (公共成員函式) | |
插入元素 或節點(C++17 起) (公共成員函式) | |
(C++23) |
插入元素範圍 (公共成員函式) |
(C++17) |
插入元素或如果鍵已存在則賦值給當前元素 (公共成員函式) |
(C++11) |
就地構造元素 (公共成員函式) |
(C++11) |
使用提示就地構造元素 (公共成員函式) |
(C++17) |
如果鍵不存在則原地插入,如果鍵存在則不執行任何操作 (公共成員函式) |
擦除元素 (公共成員函式) | |
交換內容 (公共成員函式) | |
(C++17) |
從容器中提取節點 (公共成員函式) |
(C++17) |
從另一個容器拼接節點 (公共成員函式) |
查詢 | |
返回匹配特定鍵的元素數量 (公共成員函式) | |
查詢具有特定鍵的元素 (公共成員函式) | |
(C++20) |
檢查容器是否包含具有特定鍵的元素 (公共成員函式) |
返回與特定鍵匹配的元素範圍 (公共成員函式) | |
返回指向第一個不小於給定鍵的元素的迭代器 (公共成員函式) | |
返回指向第一個大於給定鍵的元素的迭代器 (公共成員函式) | |
觀察器 | |
返回比較鍵的函式 (公共成員函式) | |
返回比較 value_type 型別物件中的鍵的函式(公共成員函式) |
[編輯] 非成員函式
(C++20中移除)(C++20中移除)(C++20中移除)(C++20中移除)(C++20中移除)(C++20起) |
按字典順序比較兩個 map 的值(函式模板) |
特化 std::swap 演算法 (函式模板) | |
(C++20) |
擦除所有滿足特定標準的元素 (函式模板) |
推導指引 |
(C++17 起) |
[編輯] 註解
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | 容器的範圍構造和插入 |
__cpp_lib_constexpr_containers |
202502L |
(C++26) | constexpr std::map |
[編輯] 示例
#include <iostream> #include <map> #include <string> #include <string_view> void print_map(std::string_view comment, const std::map<std::string, int>& m) { std::cout << comment; // Iterate using C++17 facilities for (const auto& [key, value] : m) std::cout << '[' << key << "] = " << value << "; "; // C++11 alternative: // for (const auto& n : m) // std::cout << n.first << " = " << n.second << "; "; // // C++98 alternative: // for (std::map<std::string, int>::const_iterator it = m.begin(); it != m.end(); ++it) // std::cout << it->first << " = " << it->second << "; "; std::cout << '\n'; } int main() { // Create a map of three (string, int) pairs std::map<std::string, int> m{{"CPU", 10}, {"GPU", 15}, {"RAM", 20}}; print_map("1) Initial map: ", m); m["CPU"] = 25; // update an existing value m["SSD"] = 30; // insert a new value print_map("2) Updated map: ", m); // Using operator[] with non-existent key always performs an insert std::cout << "3) m[UPS] = " << m["UPS"] << '\n'; print_map("4) Updated map: ", m); m.erase("GPU"); print_map("5) After erase: ", m); std::erase_if(m, [](const auto& pair){ return pair.second > 25; }); print_map("6) After erase: ", m); std::cout << "7) m.size() = " << m.size() << '\n'; m.clear(); std::cout << std::boolalpha << "8) Map is empty: " << m.empty() << '\n'; }
輸出
1) Initial map: [CPU] = 10; [GPU] = 15; [RAM] = 20; 2) Updated map: [CPU] = 25; [GPU] = 15; [RAM] = 20; [SSD] = 30; 3) m[UPS] = 0 4) Updated map: [CPU] = 25; [GPU] = 15; [RAM] = 20; [SSD] = 30; [UPS] = 0; 5) After erase: [CPU] = 25; [RAM] = 20; [SSD] = 30; [UPS] = 0; 6) After erase: [CPU] = 25; [RAM] = 20; [UPS] = 0; 7) m.size() = 3 8) Map is empty: true
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 230 | C++98 | 不要求 Key 為 可複製構造的 (CopyConstructible)(型別為 Key 的鍵可能無法構造) |
Key 也要求為可複製構造 (CopyConstructible) |
LWG 464 | C++98 | 透過鍵訪問 const map 不方便 |
提供了 at 函式 |
[編輯] 參閱
鍵值對的集合,按鍵排序 (類模板) | |
(C++11) |
鍵值對的集合,按鍵雜湊,鍵是唯一的 (類模板) |
(C++23) |
適配兩個容器以提供鍵值對集合,按唯一鍵排序 (類模板) |