Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to hash any type in C++. When I was writing my own map, I realized that I can't write hash function for all combinations of types (int, string, vector<int>, vector<vector<...)

do you="" have="" any="" idea?

<b="">I don't want to find an inner function. I want to create my own to figure out how it works.

What I have tried:

I had an idea to hash not data, but their addresses. But addresses can be changed, so I've got stuck.
Posted
Updated 2-Jan-22 21:02pm
Comments
Richard MacCutchan 2-Jan-22 7:07am    
There are many ways to hash data, which google will find for you. Remember all data is just a sequence of bytes, however it may be interpreted.

1 solution

You need to hash all data of any class object. And you need to overwrite it in every class which you are designing to get a unique hash.

The C++ vector has already some hash support.

If a hash isnt unique - it isnt a hash. There is no way around it.
 
Share this answer
 
Comments
Greg Utas 3-Jan-22 7:42am    
Many containers that use hashing allow for collisions.
KarstenK 3-Jan-22 9:54am    
I wont use such implementations. Hash is for lookup and I wont risk false positives.
Greg Utas 3-Jan-22 10:19am    
See std::unordered_multimap.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900