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

本文深入剖析C++ STL中的unordered_map、unordered_multimap、unordered_set和unordered_multiset,探讨它们的本质区别。通过源码分析,揭示了unordered_map与unordered_set不允许key重复,而带multi的容器允许key重复;unordered_map与unordered_set使用iterator,unordered_multimap和unordered_multiset使用const_iterator。此外,详细介绍了unordered_map的重要函数,如初始化、插入、删除、查找等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

导语

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

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

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

tem
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源代码大师

赏点狗粮吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值