std::unordered_map
的推導指南
在標頭檔案 <unordered_map> 中定義 |
||
template< class InputIt, class Hash = std::hash<iter_key_t<InputIt>>, |
(1) | (C++17 起) |
template< class Key, class T, class Hash = std::hash<Key>, class Pred = std::equal_to<Key>, |
(2) | (C++17 起) |
template< class InputIt, class Alloc > unordered_map( InputIt, InputIt, typename /*見下文*/::size_type, Alloc ) |
(3) | (C++17 起) |
template< class InputIt, class Alloc > unordered_map( InputIt, InputIt, Alloc ) |
(4) | (C++17 起) |
template< class InputIt, class Hash, class Alloc > unordered_map( InputIt, InputIt, typename /*見下文*/::size_type, Hash, Alloc ) |
(5) | (C++17 起) |
template< class Key, class T, typename Alloc > unordered_map( std::initializer_list<std::pair<Key, T>>, |
(6) | (C++17 起) |
template< class Key, class T, typename Alloc > unordered_map( std::initializer_list<std::pair<Key, T>>, Alloc ) |
(7) | (C++17 起) |
template< class Key, class T, class Hash, class Alloc > unordered_map( std::initializer_list<std::pair<Key, T>>, |
(8) | (C++17 起) |
template< ranges::input_range R, class Hash = std::hash<range_key_t<R>>, |
(9) | (C++23 起) |
template< ranges::input_range R, class Alloc > unordered_map( std::from_range_t, R&&, |
(10) | (C++23 起) |
template< ranges::input_range R, class Alloc > unordered_map( std::from_range_t, R&&, Alloc ) |
(11) | (C++23 起) |
template< ranges::input_range R, class Hash, class Alloc > unordered_map( std::from_range_t, R&&, typename /*見描述*/::size_type, |
(12) | (C++23 起) |
僅用於說明的輔助類型別名 |
||
template< class InputIter > using iter_val_t = |
(僅作說明*) | |
template< class InputIter > using iter_key_t = |
(僅作說明*) | |
template< class InputIter > using iter_mapped_t = |
(僅作說明*) | |
template< class InputIter > using iter_to_alloc_t = |
(僅作說明*) | |
template< ranges::input_range Range > using range_key_t = |
(C++23 起) (僅作說明*) |
|
template< ranges::input_range Range > using range_mapped_t = |
(C++23 起) (僅作說明*) |
|
template< ranges::input_range Range > using range_to_alloc_t = |
(C++23 起) (僅作說明*) |
|
這些過載僅在 InputIt
滿足 LegacyInputIterator、Alloc
滿足 Allocator、Hash
和 Pred
均不滿足 Allocator 且 Hash
不是整數型別時才參與過載決議。
注意:庫確定型別不滿足 LegacyInputIterator 的程度未指定,但至少整數型別不符合輸入迭代器。同樣,庫確定型別不滿足 Allocator 的程度也未指定,但至少成員型別 Alloc::value_type
必須存在,並且表示式 std::declval<Alloc&>().allocate(std::size_t{}) 在被視為未求值運算元時必須格式良好。
這些指南中的 size_type 引數型別指的是推導指南推匯出的型別的 size_type 成員型別。
[編輯] 註釋
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | 範圍感知構造和插入;過載 (9-12) |
[編輯] 示例
#include <unordered_map> int main() { // std::unordered_map m1 = {{"foo", 1}, {"bar", 2}}; // Error: braced-init-list has no type cannot // deduce pair<Key, T> from {"foo", 1} or {"bar", 2} std::unordered_map m1 = {std::pair{"foo", 2}, {"bar", 3}}; // guide #2 std::unordered_map m2(m1.begin(), m1.end()); // guide #1 }
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 3025 | C++17 | 初始化列表指南採用 std::pair<const Key, T> | 使用 std::pair<Key, T> |