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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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!