C++ 中的 map 与 hash_map

2024-08-13C/C++开发问题
1

本文介绍了C++ 中的 map 与 hash_map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我对 C++ 中的 hash_mapmap 有疑问.我知道 map 在 STL 中,但 hash_map 不是标准.两者有什么区别?

I have a question with hash_map and map in C++. I understand that map is in STL, but hash_map is not a standard. What's the difference between the two?

推荐答案

它们以非常不同的方式实现.

They are implemented in very different ways.

hash_map(TR1 和 Boost 中的 unordered_map;使用这些代替)使用哈希表,其中键被哈希到表中的一个槽,值存储在与该键关联的列表.

hash_map (unordered_map in TR1 and Boost; use those instead) use a hash table where the key is hashed to a slot in the table and the value is stored in a list tied to that key.

map 实现为平衡二叉搜索树(通常是红/黑树).

map is implemented as a balanced binary search tree (usually a red/black tree).

unordered_map 应该在访问集合的已知元素时提供稍微更好的性能,但是 map 将具有其他有用的特性(例如,它以排序顺序存储,这允许从头到尾遍历).unordered_map 在插入和删除时会比 map 更快.

An unordered_map should give slightly better performance for accessing known elements of the collection, but a map will have additional useful characteristics (e.g. it is stored in sorted order, which allows traversal from start to finish). unordered_map will be faster on insert and delete than a map.

这篇关于C++ 中的 map 与 hash_map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6