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 成员类型。
[编辑] 注解
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__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++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 3025 | C++17 | initializer-list 指引接受 std::pair<const Key, T> | 使用 std::pair<Key, T> |