名稱空間
變體
操作

std::multimap

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

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

> class multimap;
(1)
namespace pmr {

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

}
(2) (C++17 起)

std::multimap 是一個關聯容器,它包含一個已排序的鍵值對列表,並允許同一鍵有多個條目。排序是根據應用於鍵的比較函式 Compare 完成的。搜尋、插入和刪除操作具有對數複雜度。

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

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

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

鍵比較等效的鍵值對的順序是插入順序,並且不會改變。

(C++11 起)

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

std::multimap 滿足 容器 (Container)分配器感知容器 (AllocatorAwareContainer)關聯容器 (AssociativeContainer)可逆容器 (ReversibleContainer) 的要求。

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

然而,std::multimap 物件通常不能是 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 起) 表示容器節點的 節點控制代碼 (node handle) 特化[編輯]

[編輯] 成員類

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

[編輯] 成員函式

構造 multimap
(公有成員函式) [編輯]
銷燬 multimap
(公有成員函式) [編輯]
將值賦給容器
(公有成員函式) [編輯]
返回關聯的分配器
(公有成員函式) [編輯]
迭代器
(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 起)
字典序比較兩個 multimap 的值
(函式模板) [編輯]
特化 std::swap 演算法
(函式模板) [編輯]
擦除所有滿足特定標準的元素
(函式模板) [編輯]

推導指引

(C++17 起)

[編輯] 註釋

特性測試 標準 特性
__cpp_lib_containers_ranges 202202L (C++23) 容器的範圍構造和插入
__cpp_lib_constexpr_containers 202502L (C++26) constexpr std::multimap

[編輯] 示例

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 230 C++98 不要求 Key可複製構造的 (CopyConstructible)
(型別為 Key 的鍵可能無法構造)
Key 也要求
可複製構造 (CopyConstructible)

[編輯] 參閱

鍵值對集合,按鍵排序,鍵唯一
(類模板) [編輯]
鍵值對集合,按鍵雜湊
(類模板) [編輯]
適配兩個容器以提供按鍵排序的鍵值對集合
(類模板) [編輯]