如何用树数据结构有效地实现无序映射

How can unordered_map be efficiently implemented with tree data structures?

本文关键字:无序 映射 实现 有效地 何用树 数据结构      更新时间:2023-10-16

考虑unordered_map:

template<
    class Key,
    class T,
    class Hash = std::hash<Key>,
    class KeyEqual = std::equal_to<Key>,
    class Allocator = std::allocator< std::pair<const Key, T> >
> class unordered_map;

我知道(a==b)!(a<b) && !(b>a)快,但由于unordered_map不使用std::less<Key>来比较/存储映射中的密钥,我想知道一个实现如何通过树数据结构以最有效的方式读取/存储同一存储桶中的不同密钥来获利。似乎任何使用树的实现都无法避免从Key转换为定义了operator<()的KeyWrapper。

不能在unordered_map中使用树,即使只是在bucket中。unordered_map的接口根本不允许这样做。只要求密钥类型具有相等性,仅此而已。这就是为什么它被称为"无序"地图;因为不存在元素的特定排序。要使用某种二叉树,需要严格的弱排序,而这不是必需的。

如果你想使用unordered_map的变体,你可以。但它不是标准定义的unordered_map