简体   繁体   English

为什么操作符==对于std :: unordered_map而言还不够? -C ++

[英]Why is an operator== not sufficient for an std::unordered_map? - C++

Why do I need to implement both the == operator and a random operator returning a size_t? 为什么我需要同时实现==运算符和返回size_t的随机运算符? And what should the method returning size_t actually return? 返回size_t的方法实际上应该返回什么?

EDIT: When I said random operator, I didn't mean it had no use. 编辑:当我说随机运算符时,我并不是说它没有用。 What I meant is, in my eyes, I do not see what use it has, hence the last question. 我的意思是,在我看来,我看不到它有什么用,因此是最后一个问题。 7 7

A hashed container (hashtable, hashmap, unordered map) uses a hash-function to generate a single integer value to represent an index (or key) for the entry. 哈希容器(哈希表,哈希图,无序映射)使用哈希函数生成单个整数值来表示条目的索引(或键)。 This makes for a very quick-lookup, since (assuming we have a good spread of hash values) once we have the hash, we just need to look at that index. 这使您可以快速查找,因为(假设我们散列值散布得很好)一旦有了散列,我们只需要查看该索引即可。 Most other storage methods means comparing a bunch of things until the right element is found. 大多数其他存储方法意味着比较一堆东西,直到找到正确的元素。

There is really only two rules about hash-keys: 1. You get the same key for a given input each time the hash-function is called. 关于哈希键,实际上只有两个规则:1.每次调用哈希函数时,对于给定的输入,您都会获得相同的键。 2. The value is different for different input - it doesn't HAVE to be unique, but the more spread you get from similar input, the better. 2.对于不同的输入,该值是不同的-不必唯一,但是从相似的输入中获得的价差越大,效果越好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM