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 示例
基础教程推荐
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- c++ STL设置差异 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
