名稱空間
變體
操作

std::map

來自 cppreference.com
< cpp‎ | 容器
 
 
 
 
定義於標頭檔案 <map>
template<

    class Key,
    class T,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<std::pair<const Key, T>>

> class map;
(1)
namespace pmr {

    template<
        class Key,
        class T,
        class Compare = std::less<Key>
    > using map = std::map<Key, T, Compare,
                           std::pmr::polymorphic_allocator<std::pair<const Key, T>>>;

}
(2) (C++17 起)

std::map 是一個有序的關聯容器,包含具有唯一鍵的鍵值對。鍵透過比較函式 Compare 進行排序。搜尋、刪除和插入操作具有對數複雜度。Map 通常實現為紅黑樹

std::map 的迭代器按鍵的升序迭代,其中升序由用於構造的比較定義。也就是說,給定

  • m,一個 std::map
  • it_lit_r,指向 m 的可解引用迭代器,且 it_l < it_r

m.value_comp()(*it_l, *it_r) == true(如果使用預設比較,則從最小到最大)。

在標準庫使用 Compare 要求的所有地方,唯一性都透過等價關係確定。不精確地說,如果兩個物件 ab 都不比對方小,則它們被認為是等價的(不唯一):!comp(a, b) && !comp(b, a)

std::map 滿足 ContainerAllocatorAwareContainerAssociativeContainerReversibleContainer 的要求。

std::map 的所有成員函式都是 constexpr:可以在常量表達式的求值中建立和使用 std::map 物件。

然而,std::map 物件通常不能是 constexpr,因為任何動態分配的儲存都必須在同一常量表達式的求值中釋放。

(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

Allocator::pointer

(C++11 前)

std::allocator_traits<Allocator>::pointer

(C++11 起)
[編輯]
const_pointer

Allocator::const_pointer

(C++11 前)

std::allocator_traits<Allocator>::const_pointer

(C++11 起)
[編輯]
iterator LegacyBidirectionalIterator ConstexprIterator(C++26起)value_type[編輯]
const_iterator LegacyBidirectionalIteratorConstexprIterator(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>
struct /*未指定*/
{
    Iter     position;
    bool     inserted;
    NodeType node;
};

用模板引數 iteratornode_type 例項化。[編輯]

[編輯] 成員類

比較 value_type 型別的物件
(類) [編輯]

[編輯] 成員函式

構造 map
(公共成員函式) [編輯]
銷燬 map
(公共成員函式) [編輯]
將值賦給容器
(公共成員函式) [編輯]
返回關聯的分配器
(公共成員函式) [編輯]
元素訪問
訪問指定的元素,帶邊界檢查
(公共成員函式) [編輯]
訪問或插入指定元素
(公共成員函式) [編輯]
迭代器
(C++11起)
返回指向起始的迭代器
(公共成員函式) [編輯]
(C++11起)
返回指向末尾的迭代器
(公共成員函式) [編輯]
(C++11起)
返回指向起始的逆向迭代器
(公共成員函式) [編輯]
(C++11起)
返回指向末尾的逆向迭代器
(公共成員函式) [編輯]
容量
檢查容器是否為空
(公共成員函式) [編輯]
返回元素數量
(公共成員函式) [編輯]
返回元素的最大可能數量
(公共成員函式) [編輯]
修改器
清除內容
(公共成員函式) [編輯]
插入元素 或節點(C++17 起)
(公共成員函式) [編輯]
插入元素範圍
(公共成員函式) [編輯]
插入元素或如果鍵已存在則賦值給當前元素
(公共成員函式) [編輯]
(C++11)
就地構造元素
(公共成員函式) [編輯]
使用提示就地構造元素
(公共成員函式) [編輯]
如果鍵不存在則原地插入,如果鍵存在則不執行任何操作
(公共成員函式) [編輯]
擦除元素
(公共成員函式) [編輯]
交換內容
(公共成員函式) [編輯]
(C++17)
從容器中提取節點
(公共成員函式) [編輯]
(C++17)
從另一個容器拼接節點
(公共成員函式) [編輯]
查詢
返回匹配特定鍵的元素數量
(公共成員函式) [編輯]
查詢具有特定鍵的元素
(公共成員函式) [編輯]
(C++20)
檢查容器是否包含具有特定鍵的元素
(公共成員函式) [編輯]
返回與特定鍵匹配的元素範圍
(公共成員函式) [編輯]
返回指向第一個不小於給定鍵的元素的迭代器
(公共成員函式) [編輯]
返回指向第一個大於給定鍵的元素的迭代器
(公共成員函式) [編輯]
觀察器
返回比較鍵的函式
(公共成員函式) [編輯]
返回比較 value_type 型別物件中的鍵的函式
(公共成員函式) [編輯]

[編輯] 非成員函式

(C++20中移除)(C++20中移除)(C++20中移除)(C++20中移除)(C++20中移除)(C++20起)
按字典順序比較兩個 map 的值
(函式模板) [編輯]
特化 std::swap 演算法
(函式模板) [編輯]
擦除所有滿足特定標準的元素
(函式模板) [編輯]

推導指引

(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++23)
適配兩個容器以提供鍵值對集合,按唯一鍵排序
(類模板) [編輯]