in C++, how to handle hash collision in hash map? and how much time will spend to search an element if a collision occurred?
And, what is a good hash function?
There are dozens of different ways to handle collisions in hash maps depending on what system you're using. Here are a few:
The particular implementation you pick is up to you. Go with whatever is simplest. I personally find chained hashing (closed addressing) the easiest, if that suggestion helps.
As for what makes a good hash function, that's really dependent on what type of data you're storing. Hash functions for strings are often very different than hash codes for integers, for example. Depending on the security guarantees you want, you may want to pick a cryptographically secure hash like SHA-256, or just a simple heuristic like a linear combination of the individual bits. Designing a good hash function is quite tricky, and I'd advise doing a bit of digging for advice on the particular structures you're going to be hashing before coming to a conclusion.
Hope this helps!
Generally a hash map structure stores colliding elements in either a list or a tree. If they are in a list, it costs O(1) time to insert elements, but O(N) to retrieve them (N being the number of colliding elements rather than the total in the has map). If a tree is used, insertion and lookup are both O(log N).
A good hash function is one which minimizes collisions. Which function this is depends on your particular data, but in general a hash whose outputs cannot be predicted from its inputs (one which randomly scatters items across the space) is a good choice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With