C++ hash_map 和 unordered_map 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1646266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:44:47  来源:igfitidea点击:

Difference between hash_map and unordered_map?

c++stlhashmapunordered-map

提问by kidnamedlox

I recently discovered that the implementation of the hash map in C++ will be called unordered_map.

我最近发现在 C++ 中哈希映射的实现会被调用unordered_map

When I looked up why they weren't just using hash_map, I discovered that apparently there are compatibility issues with the implementation of hash_mapthat unordered_mapresolves (more about it here).

当我抬起头,为什么他们不只是使用hash_map,我发现,显然有与实施的兼容性问题hash_mapunordered_map做出决议(更多关于它在这里)。

That wiki page doesn't give much more information so I wondering if anyone knew some of the issues with hash_mapthat unordered_mapresolves.

该 wiki 页面没有提供更多信息,所以我想知道是否有人知道hash_mapunordered_map解决方案的一些问题。

回答by Stef

Since there was no hash table defined in the C++ standard library, different implementors of the standard libraries would provide a non-standard hash table often named hash_map. Because these implementations were not written following a standard they all had subtle differences in functionality and performance guarantees.

由于 C++ 标准库中没有定义哈希表,标准库的不同实现者会提供一个非标准的哈希表,通常命名为hash_map. 因为这些实现不是按照标准编写的,所以它们在功能和性能保证上都有细微的差别。

Starting with C++11a hash table implementation has been added to the C++ standard library standard. It was decided to use an alternate name for the class to prevent collisions with these non-standard implementations and to prevent inadvertent use of the new class by developers who had hash_tablein their code.

C++11开始,哈希表实现已添加到 C++ 标准库标准中。决定为该类使用一个替代名称,以防止与这些非标准实现发生冲突,并防止开发人员hash_table在其代码中无意中使用新类。

The chosen alternate name is unordered_mapwhich really is more descriptive as it hints at the class's map interface and the unordered nature of its elements.

所选择的替代名称unordered_map确实更具描述性,因为它暗示了类的地图界面及其元素的无序性质。