C++中的哈希表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/133569/
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
Hashtable in C++?
提问by Marcos Bento
I usually use C++ stdlib map whenever I need to store some data associated with a specific type of value (a key value - e.g. a string or other object). The stdlib map implementation is based on trees which provides better performance (O(log n)) than the standard array or stdlib vector.
每当我需要存储与特定类型的值(键值 - 例如字符串或其他对象)相关联的一些数据时,我通常使用 C++ stdlib 映射。stdlib 映射实现基于树,它提供比标准数组或 stdlib 向量更好的性能 (O(log n))。
My questions is, do you know of any C++ "standard" hashtable implementation that provides even better performance (O(1))? Something similar to what is available in the Hashtable class from the Java API.
我的问题是,您是否知道任何提供更好性能(O(1))的 C++“标准”哈希表实现?类似于 Java API 的 Hashtable 类中可用的内容。
回答by Chris Jester-Young
If you're using C++11, you have access to the <unordered_map>
and <unordered_set>
headers. These provide classes std::unordered_map
and std::unordered_set
.
如果您使用的是 C++11,则可以访问<unordered_map>
和<unordered_set>
标头。这些提供类std::unordered_map
和std::unordered_set
.
If you're using C++03 with TR1, you have access to the classes std::tr1::unordered_map
and std::tr1::unordered_set
, using the same headers (unless you're using GCC, in which case the headers are <tr1/unordered_map>
and <tr1/unordered_set>
instead).
如果您将 C++03 与 TR1 一起使用,则可以使用相同的标头访问类std::tr1::unordered_map
和std::tr1::unordered_set
(除非您使用 GCC,在这种情况下标头是<tr1/unordered_map>
和<tr1/unordered_set>
)。
In all cases, there are corresponding unordered_multimap
and unordered_multiset
types too.
在所有情况下,也有对应的unordered_multimap
和unordered_multiset
类型。
回答by Mark Ransom
If you don't already have unordered_map or unordered_set, they are part of boost.
Here's the documentation for both.
回答by Craig H
回答by Adam Rosenfield
Visual Studio has the class stdext::hash_map
in the header <hash_map>
, and gcc has the class __gnu_cxx::hash_map
in the same header.
Visual Studiostdext::hash_map
在 header 中<hash_map>
有类__gnu_cxx::hash_map
,gcc在同一个 header 中有类。
回答by rlbond
std::tr1::unordered_map, in <unordered_map>
std::tr1::unordered_map,在 <unordered_map>
if you don't have tr1, get boost, and use
boost::unordered_map in <boost/unordered_map.hpp>
如果您没有 tr1,请获取 boost,然后在其中使用 boost::unordered_map <boost/unordered_map.hpp>
回答by Adam Tegen
See std::hash_mapfrom SGI.
请参阅SGI 的std::hash_map。
This is included in the STLPortdistribution as well.
这也包含在STLPort发行版中。
回答by Ben Collins
回答by MSalters
If you have the TR1 extensions available for yor compiler, use those. If not, boost.org has a version that's quite similar except for the std:: namespace. In that case, put in a using-declaration so you can switch to std:: later.
如果您有可用于您的编译器的 TR1 扩展,请使用它们。如果不是,boost.org 有一个非常相似的版本,除了 std:: 命名空间。在这种情况下,请放入 using 声明,以便稍后可以切换到 std::。