I would like to see a hash_map example in C++(我想在 C++ 中查看 hash_map 示例)
问题描述
我不知道如何在 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++标准没有hash map,但是即将到来的C++0x标准有,g++已经以unordered"的形式支持了地图":
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;
}
为了得到这个编译,你需要告诉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 非常相似,除了为您自己的类型提供自定义 operator<() 之外,您需要提供自定义哈希函数 - 合适的函数是为整数和字符串等类型提供.
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 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我想在 C++ 中查看 hash_map 示例
基础教程推荐
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
