命名空间
变体
操作

std::unordered_set 的推导指南

来自 cppreference.cn
< cpp‎ | 容器‎ | 无序集合
 
 
 
 
定义于头文件 <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,
               typename /* 参见下文 */::size_type = /* 参见下文 */,
               Hash = Hash(), Pred = Pred(), Alloc = Alloc() )
    -> unordered_set<typename std::iterator_traits<InputIt>::value_type,

                     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>,
               typename /* 参见下文 */::size_type = /* 参见下文 */,
               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,
                     std::hash<typename std::iterator_traits<InputIt>::value_type>,
                     std::equal_to<typename std::iterator_traits<InputIt>::value_type>,

                     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,
                     std::equal_to<typename std::iterator_traits<InputIt>::value_type>,

                     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,
               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&&,
               typename /* 参见下文 */::size_type = /* 参见下文 */,
               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&&,
               typename /* 参见下文 */::size_type, Alloc )
    -> unordered_set<ranges::range_value_t<R>, hash<ranges::range_value_t<R>>,

                     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>>,

                     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&&,
               typename /* 参见下文 */::size_type, Hash, Alloc )
    -> unordered_set<ranges::range_value_t<R>, Hash,

                     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 满足 LegacyInputIteratorAlloc 满足 Allocator,且 HashPred 都不满足 AllocatorHash 不是整数类型时才参与重载决议。
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()
{
    // guide #2 deduces std::unordered_set<int>
    std::unordered_set s = {1, 2, 3, 4};
 
    // guide #1 deduces std::unordered_set<int>
    std::unordered_set s2(s.begin(), s.end());
}