std::unordered_map 的推导指引

 
 
 
 
定义于头文件 <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,
         typename /*see below*/::size_type = /*see below*/,
         Hash = Hash(), Pred = Pred(), Alloc = Alloc())

  -> unordered_map<iter_key_t<InputIt>, iter_value_t<InputIt>, 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<const Key, T>>,
         typename /*see below*/::size_type = /*see below*/,
         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 /*see below*/::size_type, Alloc)
  -> unordered_map<iter_key_t<InputIt>, iter_val_t<InputIt>,
           std::hash<iter_key_t<InputIt>>,

           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>,
            std::hash<iter_key_t<InputIt>>,

            std::equal_to<iter_key_t<InputIt>>, Alloc>;
(4) (C++17 起)
template<class InputIt, class Hash, class Alloc>

unordered_map(InputIt, InputIt, typename /*see below*/::size_type, Hash, Alloc)
  -> unordered_map<iter_key_t<InputIt>, iter_val_t<InputIt>, Hash,

              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<const Key, T>>,
     typename /*see below*/::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<const 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<const Key, T>>,
       typename /*see below*/::size_type, Hash, Alloc)

  -> unordered_map<Key, T, Hash, equal_to<Key>, Allocator>;
(8) (C++17 起)

其中类型别名 iter_key_titer_val_titer_to_alloc_t 如同定义如下

template<class InputIt>

using iter_key_t = std::remove_const_t<

                     typename std::iterator_traits<InputIt>::value_type::first_type>;
(仅为说明)
template<class InputIt>
using iter_val_t = typename std::iterator_traits<InputIt>::value_type::second_type;
(仅为说明)
template<class InputIt>

using iter_to_alloc_t = std::pair<
  std::add_const_t<typename std::iterator_traits<InputIt>::value_type::first_type>,
  typename std::iterator_traits<InputIt>::value_type::second_type

>
(仅为说明)

为 unordered_map 提供这些推导指引以允许从迭代器范围(重载 (1,3-5) )和 std::initializer_list (重载 (2,6-8) )推导。这些重载仅若 InputIt 满足遗留输入迭代器 (LegacyInputIterator) Alloc 满足分配器 (Allocator) HashPred 均不满足分配器 (Allocator) Hash 不是整数类型才参与重载决议。

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

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

示例

#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::initializer_list<
                        std::pair<char const* const, int>>({{"foo", 2}, {"bar", 3}}); // 指引 #2
   std::unordered_map m2(m1.begin(), m1.end()); // 指引 #1
}