std::unordered_map 的推导指引
来自cppreference.com
<tbody>
</tbody>
| 在标头 <unordered_map> 定义
|
||
template< class InputIt, class Hash = std::hash</*iter-key-t*/<InputIt>>, class Pred = std::equal_to</*iter-key-t*/<InputIt>>, class Alloc = std::allocator<iter-to-alloc-t<InputIt>> > unordered_map( InputIt, InputIt, {{#pad:|13}} typename /* 见下文 */::size_type = /* 见下文 */, {{#pad:|13}} Hash = Hash(), Pred = Pred(), Alloc = Alloc() ) -> unordered_map</*iter-key-t*/<InputIt>, /*iter-val-t*/<InputIt>, {{#pad:|13}} Hash, Pred, Alloc>; |
(1) | (C++17 起) |
template< class Key, class T, class Hash = std::hash<Key>, class Pred = std::equal_to<Key>, class Alloc = std::allocator<std::pair<const Key, T>> > unordered_map( std::initializer_list<std::pair<Key, T>>, {{#pad:|13}} typename /* 见下文 */::size_type = /* 见下文 */, {{#pad:|13}} Hash = Hash(), Pred = Pred(), Alloc = Alloc() ) -> unordered_map<Key, T, Hash, Pred, Alloc>; |
(2) | (C++17 起) |
template< class InputIt, class Alloc > unordered_map( InputIt, InputIt, typename /* 见下文 */::size_type, Alloc ) -> unordered_map</*iter-key-t*/<InputIt>, /*iter-val-t*/<InputIt>, {{#pad:|13}} std::hash</*iter-key-t*/<InputIt>>, {{#pad:|13}} std::equal_to</*iter-key-t*/<InputIt>>, Alloc>; |
(3) | (C++17 起) |
template< class InputIt, class Alloc > unordered_map( InputIt, InputIt, Alloc ) -> unordered_map</*iter-key-t*/<InputIt>, /*iter-val-t*/<InputIt>, {{#pad:|13}} std::hash</*iter-key-t*/<InputIt>>, {{#pad:|13}} std::equal_to</*iter-key-t*/<InputIt>>, Alloc>; |
(4) | (C++17 起) |
template< class InputIt, class Hash, class Alloc > unordered_map( InputIt, InputIt, typename /* 见下文 */::size_type, Hash, {{#pad:|13}} Alloc ) -> unordered_map</*iter-key-t*/<InputIt>, /*iter-val-t*/<InputIt>, Hash, {{#pad:|13}} std::equal_to</*iter-key-t*/<InputIt>>, Alloc>; |
(5) | (C++17 起) |
template< class Key, class T, typename Alloc > unordered_map( std::initializer_list<std::pair<Key, T>>, {{#pad:|13}} typename /* 见下文 */::size_type, Alloc ) -> unordered_map<Key, T, std::hash<Key>, std::equal_to<Key>, Alloc>; |
(6) | (C++17 起) |
template< class Key, class T, typename Alloc > unordered_map( std::initializer_list<std::pair<Key, T>>, Alloc ) -> unordered_map<Key, T, std::hash<Key>, std::equal_to<Key>, Alloc>; |
(7) | (C++17 起) |
template< class Key, class T, class Hash, class Alloc > unordered_map( std::initializer_list<std::pair<Key, T>>, {{#pad:|13}} typename /* 见下文 */::size_type, Hash, Alloc ) -> unordered_map<Key, T, Hash, std::equal_to<Key>, Alloc>; |
(8) | (C++17 起) |
template< ranges::input_range R, class Hash = std::hash</*range-key-t*/<R>>, class Pred = std::equal_to</*range-key-t*/<R>>, class Alloc = std::allocator</*range-to-alloc-t*/<R>> > unordered_map( std::from_range_t, R&&, {{#pad:|13}} typename /* 见下文 */::size_type = /* 见下文 */, {{#pad:|13}} Hash = Hash(), Pred = Pred(), Alloc = Alloc() ) -> unordered_map</*range-key-t*/<R>, /*range-mapped-t*/<R>, {{#pad:|13}} Hash, Pred, Alloc>; |
(9) | (C++23 起) |
template< ranges::input_range R, class Alloc > unordered_map( std::from_range_t, R&&, {{#pad:|13}} typename /* 见下文 */::size_type, Alloc ) -> unordered_map</*range-key-t*/<R>, /*range-mapped-t*/<R>, {{#pad:|13}} std::hash</*range-key-t*/<R>>, {{#pad:|13}} std::equal_to</*range-key-t*/<R>>, Alloc>; |
(10) | (C++23 起) |
template< ranges::input_range R, class Alloc > unordered_map( std::from_range_t, R&&, Alloc ) -> unordered_map</*range-key-t*/<R>, /*range-mapped-t*/<R>, {{#pad:|13}} std::hash</*range-key-t*/<R>>, {{#pad:|13}} std::equal_to</*range-key-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, {{#pad:|13}} Hash, Alloc ) -> unordered_map</*range-key-t*/<R>, /*range-mapped-t*/<R>, Hash, {{#pad:|13}} std::equal_to</*range-key-t*/<R>>, Alloc>; |
(12) | (C++23 起) |
| 仅用于阐释的辅助类型别名 |
||
template< class InputIt > using /*iter-val-t*/ = typename std::iterator_traits<InputIt>::value_type; |
(仅用于阐述*) | |
template< class InputIt > using /*iter-key-t*/ = std::remove_const_t< std::tuple_element_t<0, /*iter-val-t*/<InputIt>>>; |
(仅用于阐述*) | |
template< class InputIt > using /*iter-mapped-t*/ = std::tuple_element_t<1, /*iter-val-t*/<InputIt>>; |
(仅用于阐述*) | |
template< class InputIt > using /*iter-to-alloc-t*/ = std::pair<std::add_const_t<tuple_element_t<0, /*iter-val-t*/<InputIt>>>, std::tuple_element_t<1, /*iter-val-t*/<InputIt>>>; |
(仅用于阐述*) | |
template< ranges::input_range Range > using /*range-key-t*/ = std::remove_const_t<typename ranges::range_value_t<Range>::first_type>; |
(C++23 起) (仅用于阐述*) |
|
template< ranges::input_range Range > using /*range-mapped-t*/ = typename ranges::range_value_t<Range>::second_type; |
(C++23 起) (仅用于阐述*) |
|
template< ranges::input_range Range > using /*range-to-alloc-t*/ = std::pair<std::add_const_t<typename ranges::range_value_t<Range>::first_type>, typename ranges::range_value_t<Range>::second_type>; |
(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}};
// 错误:花括号初始化式列表无类型
// 不能从 {"foo", 1} 或 {"bar", 2} 推导 pair<const Key, T>
std::unordered_map m1 = {std::pair{"foo", 2}, {"bar", 3}}; // 指引 #2
std::unordered_map m2(m1.begin(), m1.end()); // 指引 #1
}
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 3025 | C++17 | 初始化式列表指引 (2) 和 (6-8) 接受 std::pair<const Key, T>
|
使用 std::pair<Key, T>
|