我想在C ++中看到一个hash_map示例 [英] I would like to see a hash_map example in C++
问题描述
我不知道如何使用C ++中的哈希函数,但我知道我们可以使用 hash_map
。 g ++支持简单地包括 #include< hash_map>
?什么是一个简单的例子使用 hash_map
I don't know how to use the hash function in C++, but I know that we can use hash_map
. Does g++ support that by simply including #include <hash_map>
? What is a simple example using hash_map
?
推荐答案
没有哈希映射,但是来自C ++ 0x标准,并且这些已经由g ++以无序映射的形式支持:
The current C++ standard does not have hash maps, but the coming C++0x standard does, and these are already supported by g++ in the shape of "unordered maps":
#include <unordered_map>
#include <iostream>
#include <string>
using namespace std;
int main() {
unordered_map <string, int> m;
m["foo"] = 42;
cout << m["foo"] << endl;
}
$ b $ p
为了得到这个编译,你需要告诉g ++你正在使用C ++ 0x:
In order to get this compile, you need to tell g++ that you are using C++0x:
g++ -std=c++0x main.cpp
这些地图的工作方式与std :: map一样,只是不是提供一个自定义的运算符< code>为你自己的类型,你需要提供一个自定义哈希函数 - 为类似整数和字符串提供适当的函数。
These maps work pretty much as std::map does, except that instead of providing a custom operator<()
for your own types, you need to provide a custom hash function - suitable functions are provided for types like integers and strings.
这篇关于我想在C ++中看到一个hash_map示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!