std::unordered_set 的推导指引

来自cppreference.com

[编辑模板]
 
 
 
 
<tbody> </tbody>
在标头 <unordered_set> 定义
template< class InputIt, class Hash = std::hash<typename std::iterator_traits<InputIt>::value_type>, class Pred = std::equal_to<typename std::iterator_traits<InputIt>::value_type>, class Alloc = std::allocator<typename std::iterator_traits<InputIt>::value_type> > unordered_set( InputIt, InputIt, {{#pad:|13}} typename /* 见下文 */::size_type = /* 见下文 */, {{#pad:|13}} Hash = Hash(), Pred = Pred(), Alloc = Alloc() ) -> unordered_set<typename std::iterator_traits<InputIt>::value_type, {{#pad:|13}} Hash, Pred, Alloc>;
(1) (C++17 起)
template< class T, class Hash = std::hash<T>, class Pred = std::equal_to<T>, class Alloc = std::allocator<T> > unordered_set( std::initializer_list<T>, {{#pad:|13}} typename /* 见下文 */::size_type = /* 见下文 */, {{#pad:|13}} Hash = Hash(), Pred = Pred(), Alloc = Alloc() ) -> unordered_set<T, Hash, Pred, Alloc>;
(2) (C++17 起)
template< class InputIt, class Alloc > unordered_set( InputIt, InputIt, typename /* 见下文 */::size_type, Alloc ) -> unordered_set<typename std::iterator_traits<InputIt>::value_type, {{#pad:|13}} std::hash<typename std::iterator_traits<InputIt>::value_type>, {{#pad:|13}} std::equal_to<typename std::iterator_traits<InputIt>::value_type>, {{#pad:|13}} Alloc>;
(3) (C++17 起)
template< class InputIt, class Hash, class Alloc > unordered_set( InputIt, InputIt, typename /* 见下文 */::size_type, Hash, Alloc ) -> unordered_set<typename std::iterator_traits<InputIt>::value_type, Hash, {{#pad:|13}} std::equal_to<typename std::iterator_traits<InputIt>::value_type>, {{#pad:|13}} Alloc>;
(4) (C++17 起)
template< class T, class Alloc > unordered_set( std::initializer_list<T>, typename /* 见下文 */::size_type, Alloc ) -> unordered_set<T, std::hash<T>, std::equal_to<T>, Alloc>;
(5) (C++17 起)
template< class T, class Hash, class Alloc > unordered_set( std::initializer_list<T>, typename /* 见下文 */::size_type, {{#pad:|13}} Hash, Alloc ) -> unordered_set<T, Hash, std::equal_to<T>, Alloc>;
(6) (C++17 起)
template< ranges::input_range R, class Hash = std::hash<ranges::range_value_t<R>>, class Pred = std::equal_to<ranges::range_value_t<R>>, class Alloc = std::allocator<ranges::range_value_t<R>> > unordered_set( std::from_range_t, R&&, {{#pad:|13}} typename /* 见下文 */::size_type = /* 见下文 */, {{#pad:|13}} Hash = Hash(), Pred = Pred(), Alloc = Alloc() ) -> unordered_set<ranges::range_value_t<R>, Hash, Pred, Alloc>;
(7) (C++23 起)
template< ranges::input_range R, class Alloc > unordered_set( std::from_range_t, R&&, {{#pad:|13}} typename /* 见下文 */::size_type, Alloc ) -> unordered_set<ranges::range_value_t<R>, hash<ranges::range_value_t<R>>, {{#pad:|13}} std::equal_to<ranges::range_value_t<R>>, Alloc>;
(8) (C++23 起)
template< ranges::input_range R, class Alloc > unordered_set( std::from_range_t, R&&, Alloc ) -> unordered_set<ranges::range_value_t<R>, hash<ranges::range_value_t<R>>, {{#pad:|13}} std::equal_to<ranges::range_value_t<R>>, Alloc>;
(9) (C++23 起)
template< ranges::input_range R, class Hash, class Alloc > unordered_set( std::from_range_t, R&&, {{#pad:|13}} typename /* 见下文 */::size_type, Hash, Alloc ) -> unordered_set<ranges::range_value_t<R>, Hash, {{#pad:|13}} std::equal_to<ranges::range_value_t<R>>, Alloc>;
(10) (C++23 起)
1-6) 为 unordered_set 提供了这些推导指引,以允许从迭代器范围(重载 (1,3,4))和 std::initializer_list(重载 (2,5.6))推导。此重载只有在 InputIt 满足老式输入迭代器 (LegacyInputIterator) Alloc 满足分配器 (Allocator) HashPred 均不满足分配器 (Allocator) Hash 不是整数类型时才会参与重载决议。
7-10) 为 unordered_set 提供了这些推导指引,以允许从 std::from_range_t 标签和一个 input_range 推导。

注意:库确定类型是否满足老式输入迭代器 (LegacyInputIterator) 的程度是未指定的,但最低要求是整数类型不具备输入迭代器的条件。类似地,确定类型是否满足分配器 (Allocator) 是未指定的,但最低要求是成员类型 Alloc::value_type 必须存在,且表达式 std::declval<Alloc&>().allocate(std::size_t{}) 在作为不求值操作数时必须为良构。

这些指引中的 size_type 形参类型指代推导指引所推导出的类型的 size_type 成员类型。

注解

功能特性测试 标准 功能特性
__cpp_lib_containers_ranges 202202L (C++23) 按范围构造和插入; 重载 (7-10)

示例

#include <unordered_set>

int main()
{
    // 指引 #2 推导 std::unordered_set<int>
    std::unordered_set s = {1, 2, 3, 4};

    // 指引 #1 推导 std::unordered_set<int>
    std::unordered_set s2(s.begin(), s.end());
}