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

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

Hashtable in C++?

c++performancemaphashtablecomplexity-theory

提问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_mapand std::unordered_set.

如果您使用的是 C++11,则可以访问<unordered_map><unordered_set>标头。这些提供类std::unordered_mapstd::unordered_set.

If you're using C++03 with TR1, you have access to the classes std::tr1::unordered_mapand 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_mapstd::tr1::unordered_set(除非您使用 GCC,在这种情况下标头是<tr1/unordered_map><tr1/unordered_set>)。

In all cases, there are corresponding unordered_multimapand unordered_multisettypes too.

在所有情况下,也有对应的unordered_multimapunordered_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.

如果您还没有 unordered_map 或 unordered_set,它们是boost 的一部分。
这是两者的文档

回答by Craig H

There is a hash_mapobject as many here have mentioned, but it is not part of the stl. It is a SGI extension, so if you were looking for something in the STL, I think you are out of luck.

这里有很多人提到的hash_map对象,但它不是 stl 的一部分。它是一个 SGI 扩展,所以如果你在 STL 中寻找一些东西,我认为你不走运。

回答by Adam Rosenfield

Visual Studio has the class stdext::hash_mapin the header <hash_map>, and gcc has the class __gnu_cxx::hash_mapin 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

hash_map is also supported in GNU's libstdc++.

GNU 的libstdc++也支持 hash_map 。

Dinkumware also supportsthis, which means that lots of implementations will have a hash_map (I think even Visual C++ delivers with Dinkumware).

Dinkumware 也支持这一点,这意味着很多实现都会有一个 hash_map(我认为甚至 Visual C++ 也提供了 Dinkumware)。

回答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::。