std::unordered_set
的推导指引
在标头
<unordered_set>
定义
|
||
template<class
InputIt,
class
Hash =
std::hash<typename
std::iterator_traits<InputIt>::value_type>, |
(1) | (C++17 起) |
template<class
T,
class
Hash =
std::hash<T>, |
(2) | (C++17 起) |
template<class
InputIt, class
Alloc>
unordered_set(InputIt,
InputIt, typename
/*see
below*/::size_type,
Alloc) |
(3) | (C++17 起) |
template<class
InputIt, class
Hash, class
Alloc>
unordered_set(InputIt,
InputIt, typename
/*see
below*/::size_type,
Hash, Alloc) |
(4) | (C++17 起) |
template<class
T, class
Allocator>
unordered_set(std::initializer_list<T>,
typename
/*see
below*/::size_type,
Allocator) |
(5) | (C++17 起) |
template<class
T, class
Hash, class
Alloc>
unordered_set(std::initializer_list<T>,
typename
/*see
below*/::size_type,
Hash, Alloc) |
(6) | (C++17 起) |
为 unordered_set 提供这些推导指引以允许从迭代器范围(重载
(1,3,4) )和 std::initializer_list
(重载 (2,5.6) )推导。此重载只有在 InputIt
满足老式输入迭代器 (LegacyInputIterator)
、 Alloc
满足分配器 (Allocator)
、 Hash
或 Pred
均不满足分配器 (Allocator)
、 Hash
不是整数类型时才会参与重载决议
注意:库确定类型是否满足老式输入迭代器 (LegacyInputIterator)
的程度是未指定的,除了最低要求是整数类型不具备输入迭代器的条件。类似地,确定类型是否满足分配器 (Allocator)
是未指定的,除了最低要求是成员类型 Alloc::value_type
必须存在,且表达式 std::declval<Alloc&>().allocate(std::size_t{})
在作为不求值操作数时必须为良构。
这些指引中的 size_type 参数类型指代推导指引所推导出的类型的 size_type 成员类型。
示例
#include <unordered_set> int main() { std::unordered_set s = {1,2,3,4}; // 指引 #2 推导 std::unordered_set<int> std::unordered_set s2(s.begin(), s.end()); // 指引 #1 推导 std::unordered_set<int> }