CodeRoadMap
路线图学习路径文章题库资源社区

浏览

首页路线图学习路径知识库题库文章资源社区我的学习
CodeRoadMap

中文编程学习导航:路线图、讲义与题库,进度可同步。

路线图学习路径文章题库社区

© 2026 CodeRoadMap

津ICP备2026012044号-1|coderoadmap@126.com
首页/C++ 那些事/C++ STL源码剖析之unordered_map、unordered_multimap、unordered_set、unordered_multiset

课程目录

  1. 01const 那些事
  2. 02static 那些事
  3. 03this 那些事
  4. 04inline 那些事
  5. 05sizeof 那些事
  6. 06纯虚函数和抽象类那些事
  7. 07vptr_vtable 那些事
  8. 08virtual 那些事
  9. 09volatile 那些事
  10. 10assert 那些事
  11. 11位域那些事
  12. 12extern 那些事
  13. 13struct 那些事
  14. 14struct 与 class 那些事
  15. 15union 那些事
  16. 16用 C 实现 C++ 多态
  17. 17explicit 那些事
  18. 18friend 那些事
  19. 19using 那些事
  20. 20:: 那些事
  21. 21enum 那些事
  22. 22decltype 那些事
  23. 23引用与指针那些事
  24. 24宏那些事
  25. 25C++11 新特性
  26. 26C++ STL源码剖析 tr1与std array
  27. 27C++ STL源码剖析之序列式容器deque
  28. 28C++ STL源码剖析之哈希表
  29. 29C++ STL源码剖析之实现一个简单的iterator_category
  30. 30C++ STL源码剖析之双向环形链表list
  31. 31C++ STL源码剖析之map、multimap、initializer_list
  32. 32从0到1打牢算法基础之手写一个哈希表
  33. 33C++ STL源码剖析之容器配接器stack与queue、priority_queue
  34. 34C++ STL源码剖析之红黑树
  35. 35STL之set与multiset那些事
  36. 36C++ STL 源码剖析之 Traits 编程技法
  37. 37typename
  38. 38C++ STL源码剖析之unordered_map、unordered_multimap、unordered_set、unordered_multiset
  39. 39STL源码剖析之vector
  40. 40STL设计之EBO(空基类优化)
  41. 41设计模式:单例模式
  42. 42初始化列表与赋值
  43. 43C++惯用法之enum class
  44. 44C++惯用法之消除垃圾收集器-资源获取即初始化方法(RAII)
  45. 45copy-swap 惯用法
  46. 46C++惯用法之pImpl
  47. 4710日c++实战狂练
  48. 48Day4 · const 练习
  49. 49��Ԫ���
  50. 50Day5 · 构造与析构
  51. 51Day5 · 继承与访问控制
  52. 52Day5 · 运算符重载
  53. 53Day5 · 三五法则
  54. 54Day5 · virtual 练习
  55. 55Day6 · 综合练习
  56. 56Day7 · 综合练习
  57. 57Day8 · 综合练习
  58. 58异常处理
  59. 59Day10 · 综合练习
  60. 60重点实战练习
第 38 课1.5 小时

C++ STL源码剖析之unordered_map、unordered_multimap、unordered_set、unordered_multiset

前面学到了hashtable,而这节是hashtable的容器适配器:unordered_map。

课程内容

C++ STL源码剖析之unordered_map、unordered_multimap、unordered_set、unordered_multiset

0.导语

前面学到了hashtable,而这节是hashtable的容器适配器:unordered_map。

所以无序map的底层容器采用hashtable。

unordered_map与unordered_multimap的源码在unordered_map.h这个文件中。

1.unordered_map与unordered_multimap本质区别

先来看一下unordered_map源码:

template<class _Key, class _Tp,
class _Hash = hash<_Key>,
class _Pred = std::equal_to<_Key>,
class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
class unordered_map
{
    typedef __umap_hashtable<_Key, _Tp, _Hash, _Pred, _Alloc>  _Hashtable;
    _Hashtable _M_h;
};

去看底层容器的__umap_hashtable的声明:

template<bool _Cache>
using __umap_traits = __detail::_Hashtable_traits<_Cache, false, true>;

template<typename _Key,
    typename _Tp,
    typename _Hash = hash<_Key>,
    typename _Pred = std::equal_to<_Key>,
    typename _Alloc = std::allocator<std::pair<const _Key, _Tp> >,
    typename _Tr = __umap_traits<__cache_default<_Key, _Hash>::value>>
using __umap_hashtable = _Hashtable<_Key, std::pair<const _Key, _Tp>,
_Alloc, __detail::_Select1st,
_Pred, _Hash,
__detail::_Mod_range_hashing,
__detail::_Default_ranged_hash,
__detail::_Prime_rehash_policy, _Tr>;

可以得到下面结论: hashtable的模板参数:

template<typename _Key, typename _Value, typename _Alloc,
typename _ExtractKey, typename _Equal,
typename _H1, typename _H2, typename _Hash,
typename _RehashPolicy, typename _Traits>

默认情况下,unordered_map采用:

  • H1为hash<key>
  • H2为_Mod_range_hashing
  • _Hash为_Default_ranged_hash
  • _RehashPolicy为_Prime_rehash_policy
  • _Traits为_Tr 对于最后的_Tr,非常重要,因为正是因为这个参数,才有unordered_multimap。 具体分析看下面:

_Tr如下:

typename _Tr = __umap_traits<__cache_default<_Key, _Hash>::value>>

_Tr使用了__umap_traits,我们继续往下看:


template<bool _Cache>
using __umap_traits = __detail::_Hashtable_traits<_Cache, false, true>;

可以对比上述两个发现__umap_traits里面有一串__cache_default,我们再看一下模板参数为bool类型,故可以打印出来是false还是true,经过实测,为false,表示不缓存hash code。

template<typename _Tp, typename _Hash>
using __cache_default
=  __not_<__and_<__is_fast_hash<_Hash>,
__detail::__is_noexcept_hash<_Tp, _Hash>>>;

继续看__umap_traits,这个实际上是调用_Hashtable_traits,我们继续往下:

template<bool _Cache_hash_code, bool _Constant_iterators, bool _Unique_keys>
struct _Hashtable_traits
{
    using __hash_cached = __bool_constant<_Cache_hash_code>;
    using __constant_iterators = __bool_constant<_Constant_iterators>;
    using __unique_keys = __bool_constant<_Unique_keys>;
};

看到有三个using,理解为三个typedef,依次表示:hash code缓存与否,是否是常迭代器,是否是唯一的key,再往上回头看,传递进来的是三个模板参数,分别是false,false,true,也验证了unordered_map是唯一的key,那么对应的unordered_multimap就是不唯一的key,最后一个参数为false。我们翻阅到相应代码如下:

/// Base types for unordered_multimap.
template<bool _Cache>
using __ummap_traits = __detail::_Hashtable_traits<_Cache, false, false>;

小结,在上面分析,我们知道了unordered_map与unordered_multimap的本质区别,也发现了如何在底层源码上用一个容器实现两个容器适配器!

2.unordered_set与unordered_multiset本质区别

分析同前面一样,先看unordered_set:

template<class _Value,
    class _Hash = hash<_Value>,
    class _Pred = std::equal_to<_Value>,
    class _Alloc = std::allocator<_Value> >
class unordered_set
{
    typedef __uset_hashtable<_Value, _Hash, _Pred, _Alloc>  _Hashtable;
    _Hashtable _M_h;
}

template<bool _Cache>
using __uset_traits = __detail::_Hashtable_traits<_Cache, true, true>;

template<typename _Value,
    typename _Hash = hash<_Value>,
    typename _Pred = std::equal_to<_Value>,
    typename _Alloc = std::allocator<_Value>,
    typename _Tr = __uset_traits<__cache_default<_Value, _Hash>::value>>
using __uset_hashtable = _Hashtable<_Value, _Value, _Alloc,
                __detail::_Identity, _Pred, _Hash,
                __detail::_Mod_range_hashing,
                __detail::_Default_ranged_hash,
                __detail::_Prime_rehash_policy, _Tr>;

可以看到传递给_Hashtable_traits的是false,true,true。对于unordered_set来说使用的是const iterator与唯一的key,我们再看一下unordered_multiset:

template<bool _Cache>
using __umset_traits = __detail::_Hashtable_traits<_Cache, true, false>;

再将两者对比一下,本质就是unordered_set不允许key重复,而unordered_multiset允许key重复。

3.三大结论

现在,我们有了前面基础,依次列出前面四个容器适配器:

(1) unordered_map

template<bool _Cache>
using __umap_traits = __detail::_Hashtable_traits<_Cache, false, true>;

(2) unordered_multimap

template<bool _Cache>
using __umap_traits = __detail::_Hashtable_traits<_Cache, false, false>;

(3) unordered_set

template<bool _Cache>
using __uset_traits = __detail::_Hashtable_traits<_Cache, true, true>;

(4) unordered_multiset

template<bool _Cache>
using __uset_traits = __detail::_Hashtable_traits<_Cache, true, false>;

对比后,得出

  • 结论1:unordered_map与unordered_set不允许key重复,而带multi的则允许key重复;
  • 结论2:unordered_map与unordered_multimap采用的迭代器是iterator,而unordered_set与unordered_multiset采用的迭代器是const_iterator。
  • 结论3:unordered_map与unordered_multimap的key是key,value是key+value;而unordered_set与unordered_multiset的key是Value,Value也是Key。

最后一个结论对比看下面(我们看传递给hashtable的第一与第二个参数):

unordered_map与unordered_multimap:

using __umap_hashtable = _Hashtable<_Key, 
std::pair<const _Key, _Tp>,
_Alloc, __detail::_Select1st,
_Pred, _Hash,
__detail::_Mod_range_hashing,
__detail::_Default_ranged_hash,
__detail::_Prime_rehash_policy, _Tr>;

unordered_set与unordered_multiset:

template<typename _Value,
typename _Hash = hash<_Value>,
typename _Pred = std::equal_to<_Value>,
typename _Alloc = std::allocator<_Value>,
typename _Tr = __uset_traits<__cache_default<_Value, _Hash>::value>>
using __uset_hashtable = _Hashtable<_Value, _Value, _Alloc,
__detail::_Identity, _Pred, _Hash,
__detail::_Mod_range_hashing,
__detail::_Default_ranged_hash,
__detail::_Prime_rehash_policy, _Tr>;

4.unordered_map重要函数

初始化

可以在下面的构造函数中看到unordered_map的默认桶数为10。

在unordered_map的底层默认采用hasher(),也就是H1,也就是std::hash

unordered_map(size_type __n = 10,
    const hasher& __hf = hasher(),
    const key_equal& __eql = key_equal(),
    const allocator_type& __a = allocator_type())
: _M_h(__n, __hf, __eql, __a)
{ }

下面测试是否采用默认的hash:

unordered_map<string,int> um;
hash<string> h;
cout<<um.hash_function()("hhhhhawq")<<endl;
cout<<h("hhhhhawq")<<endl;

输出:

9074142923776869151
9074142923776869151

进一步验证了采用默认的hash。

是否空、大小、最大大小

bool
empty() const noexcept
{ return _M_h.empty(); }
///  Returns the size of the %unordered_map.
size_type
size() const noexcept
{ return _M_h.size(); }

///  Returns the maximum size of the %unordered_map.
size_type
max_size() const noexcept
{ return _M_h.max_size(); }

begin与end

iterator
begin() noexcept
{ return _M_h.begin(); }
iterator
end() noexcept
{ return _M_h.end(); }

insert 五种插入方式

// value
std::pair<iterator, bool>
insert(const value_type& __x)
{ return _M_h.insert(__x); }

// pair 
std::pair<iterator, bool>
insert(_Pair&& __x)
{ return _M_h.insert(std::forward<_Pair>(__x)); }

// iterator+value
iterator
insert(const_iterator __hint, const value_type& __x)
{ return _M_h.insert(__hint, __x); }


// first到last范围插入
template<typename _InputIterator>
void
insert(_InputIterator __first, _InputIterator __last)
{ _M_h.insert(__first, __last); }

// 初始化列表插入

void
insert(initializer_list<value_type> __l)
{ _M_h.insert(__l); }

删除

三种删除方式

// iterator
iterator
erase(iterator __position)
{ return _M_h.erase(__position); }

// key
size_type
erase(const key_type& __x)
{ return _M_h.erase(__x); }

// first到last范围

iterator
erase(const_iterator __first, const_iterator __last)
{ return _M_h.erase(__first, __last); 

清除

void
clear() noexcept
{ _M_h.clear(); }

hash_function

得到该unordered_map的hash_function

hasher
hash_function() const
{ return _M_h.hash_function(); }

使用:

unordered_map<string,int> um;
cout<<um.hash_function()("hhhhhawq")<<endl; //传递的内容要与上面key类型一致。

key_eq key_eq返回的是std::equal_to 使用如下:

unordered_map<string,int> um;
cout<<um.key_eq()("1","2")<<endl;

查找与获取value

iterator
find(const key_type& __x)
{ return _M_h.find(__x); }
mapped_type&
operator[](const key_type& __k)
{ return _M_h[__k]; }
mapped_type&
at(const key_type& __k)
{ return _M_h.at(__k); }

除了这些函数还有获取桶,最大桶数、加载因子、rehash等等,就是没有排序,因为hashtable没有提供排序功能。hashtable在查找、删除和插入节点是常数时间,优于RB-Tree红黑树。

同理,unordered_set、unordered_multiset、unordered_multimap与unordered_map一样的函数,所以就不阐述了。

← 上一课typename下一课 →STL源码剖析之vector