c++ - unordered_map 中用户定义类型的运算符重载()

标签 c++ stl hashmap unordered-map

我正在看这篇文章C++ unordered_map using a custom class type as the key

  1. 我了解我们需要为自定义类型键重新定义相等哈希码
  2. 我知道运算符重载的一般工作原理。

但是,operator()哈希码 有什么关系?
unordered_map 是否在内部使用 () 运算符在某处计算键?

最佳答案

std::unordered_map使用std::hash对象来计算哈希值。

它将像函数一样使用哈希对象,调用 operator()进行计算。

作为一个简单的例子,对于 int 键,它将是这样的:

int key = 123;  // Or whatever value...

std::hash<int> hash_object;
auto hash_value = hash_object(key);  // Calls hash_object.operator()(key)

关于c++ - unordered_map 中用户定义类型的运算符重载(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74347125/

相关文章:

c++ - memmem() STL 方式?

C++ STL 集 : Compare object with extrinsic state

java-native-interface - JNI : Create HashMap

c++ - OpenGL glGenVertexArray() 异常

c++ - 简单 move 参数时使用函数模板参数的优点

c++ - 动态创建继承类,使用 std::map 使用基类指针访问?

java - 迭代 Hashmap 时如何得到 ConcurrentModificationException?

caching - 在Spark Streaming中,我们可以将数据(hashmap)存储在Executor内存中吗

c++ - 为什么使用绑定(bind)而不是函数调用?

c++ - 哪个目标文件包含以下静态模板化 "member variable"?