简体   繁体   中英

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? And what should the method returning size_t actually return?

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

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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