命名空間
變體
動作

std::map

出自 cppreference.com
< cpp‎ | container
 
 
 
 
定義於標頭檔 <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 是一種已排序的關聯式容器,包含具有唯一鍵(key)的鍵值對(key-value pairs)。鍵會使用比較函式 Compare 進行排序。搜尋、移除與插入操作具有對數複雜度。Map 通常以 紅黑樹 (Red–black trees) 實作。

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 符合 容器 (Container)具分配器容器 (AllocatorAwareContainer)關聯式容器 (AssociativeContainer)可逆容器 (ReversibleContainer) 的需求。

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 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 起) 代表容器節點的 節點控制代碼 (node handle) 特化版本[編輯]
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++17 起)
(公開成員函式) [編輯]
插入數值範圍元素
(公開成員函式) [編輯]
插入元素,若鍵已存在則指派給現有元素
(公開成員函式) [編輯]
(C++11)
原地建構元素
(公開成員函式) [編輯]
使用提示(hint)原地建構元素
(公開成員函式) [編輯]
若鍵不存在則原地插入;若鍵已存在則不執行任何操作
(公開成員函式) [編輯]
刪除元素
(公開成員函式) [編輯]
交換內容
(公開成員函式) [編輯]
(C++17)
從容器中提取節點
(公開成員函式) [編輯]
(C++17)
從另一個容器接合(splice)節點
(公開成員函式) [編輯]
查找
回傳符合指定鍵的元素數量
(公開成員函式) [編輯]
尋找具有指定鍵的元素
(公開成員函式) [編輯]
(C++20)
檢查容器是否包含具有指定鍵的元素
(公開成員函式) [編輯]
回傳與特定鍵相匹配的元素範圍
(公開成員函式) [編輯]
回傳指向第一個「不小於」給定鍵之元素的迭代器
(公開成員函式) [編輯]
回傳指向第一個「大於」給定鍵之元素的迭代器
(公開成員函式) [編輯]
觀察器
回傳比較鍵的函式
(公開成員函式) [編輯]
回傳比較 value_type 型別物件中鍵的函式
(公開成員函式) [編輯]

[編輯] 非成員函式

(於 C++20 中移除)(於 C++20 中移除)(於 C++20 中移除)(於 C++20 中移除)(於 C++20 中移除)(自 C++20 起)
按字典序比較兩個 map 的值
(函式模板) [edit]
特化 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++ 標準。

DR 應用於 出版時的行為 正確的行為
LWG 230 C++98 Key 不強制要求為 可複製建構 (CopyConstructible)
(Key 型別的鍵可能無法被建構)
Key 也被要求必須
CopyConstructible
LWG 464 C++98 透過鍵存取 const map 較為不便 提供 at 函式

[編輯] 參見

鍵值對的集合,按鍵排序
(類別樣板) [編輯]
鍵值對的集合,按鍵雜湊,鍵是唯一的
(類別範本) [編輯]
(C++23)
適配兩個容器以提供按唯一鍵排序的鍵值對集合
(類別樣板) [編輯]
English Deutsch 日本語 中文(简体) 中文(繁體)