C++中简单的hashmap实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/266206/
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 14:09:26  来源:igfitidea点击:

Simple hashmap implementation in C++

c++hashmaphashtable

提问by Paulo Guedes

I'm relatively new to C++. In Java, it's easy for me to instantiate and use a hashmap. I'd like to know how to do it in a simple way in C++, since I saw many different implementations and none of them looked simple to me.

我对 C++ 比较陌生。在 Java 中,我很容易实例化和使用哈希图。我想知道如何在 C++ 中以简单的方式完成它,因为我看到了许多不同的实现,但对我来说没有一个看起来很简单。

采纳答案by hazzen

Most compilers should define std::hash_mapfor you; in the coming C++0xstandard, it will be part of the standard library as std::unordered_map. The STL Pageon it is fairly standard. If you use Visual Studio, Microsofthas a page on it.

大多数编译器应该std::hash_map为你定义;在即将到来的C++0x标准中,它将作为std::unordered_map. 它上面的STL 页面是相当标准的。如果您使用 Visual Studio,Microsoft 上有一个页面。

If you want to use your class as the value, not as the key, then you don't need to do anything special. All primitive types (things like int, char, booland even char *) should "just work" as keys in a hash_map. However, for anything else you will have to define your own hashing and equality functions and then write "functors" that wrap them in a class.

如果你想用你的类作为值,而不是作为键,那么你不需要做任何特别的事情。所有原始类型(例如intcharbool甚至char *)都应该“正常工作”作为hash_map. 但是,对于其他任何事情,您都必须定义自己的散列和相等函数,然后编写将它们包装在一个类中的“函子”。

Assuming your class is called MyClassand you have already defined:

假设你的类被调用MyClass并且你已经定义了:

size_t MyClass::HashValue() const { /* something */ }
bool MyClass::Equals(const MyClass& other) const { /* something */ }

You will need to define two functors to wrap those methods in objects.

您将需要定义两个函子来将这些方法包装在对象中。

struct MyClassHash {
  size_t operator()(const MyClass& p) const {
    return p.HashValue();
  }
};

struct MyClassEqual {
  bool operator()(const MyClass& c1, const MyClass& c2) const {
    return c1.Equals(c2);
  }
};

And instantiate your hash_map/hash_setas:

并将您的hash_map/实例hash_set化为:

hash_map<MyClass, DataType, MyClassHash, MyClassEqual> my_hash_map;
hash_set<MyClass, MyClassHash, MyClassEqual> my_hash_set;

Everything should work as expected after that.

之后一切都应该按预期工作。

回答by Kasprzol

Using hashmaps in C++ is easy! It's like using standard C++ map. You can use your's compiler/library implementation of unordered_mapor use the one provided by boost, or some other vendor. Here's a quick sample. You will find more if you follow the links you were given.

在 C++ 中使用哈希图很容易!这就像使用标准的 C++ 映射。您可以使用自己的编译器/库实现unordered_map或使用boost或其他一些供应商提供的实现。这是一个快速示例。如果您按照提供的链接操作,您会发现更多信息。

#include <unordered_map>
#include <string>
#include <iostream>

int main()
{
    typedef std::tr1::unordered_map< std::string, int > hashmap;
    hashmap numbers;

    numbers["one"] = 1;
    numbers["two"] = 2;
    numbers["three"] = 3;

    std::tr1::hash< std::string > hashfunc = numbers.hash_function();
    for( hashmap::const_iterator i = numbers.begin(), e = numbers.end() ; i != e ; ++i ) {
        std::cout << i->first << " -> " << i->second << " (hash = " << hashfunc( i->first ) << ")" << std::endl;
    }
    return 0;
}

回答by dalle

Take a look at boost.unordered, and its data structure.

看看boost.unordered及其数据结构

回答by Mark Ransom

Try boost's unorderedclasses.

试试 boost 的无序类。

回答by aozturk

Check out Simple Hash Map (Hash Table) Implementation in C++for a basic Hash Table with generic type key-value pairs and separate chaining strategy.

查看C++中的简单哈希映射(哈希表)实现,了解具有通用类型键值对和单独链接策略的基本哈希表。