For example if i have to store many entries like in C
hash["1"]="11"
hash["2"]="12"
hash["11"]="21"
Condition is that : You only look for consecutive identical numbers, so only keep those in the map.
And like these many entries for solving a problem. We can use some technique such as adding the sum of ASCII values for finding the index and then using some mod function.
But this approach doesn’t guarantee that there will be no collision and even if there is one collision then the whole question will go wrong.
Can this be accomplished in C++ easily?
Please give some suggestions/hints.
Thanks in Advance
Regarding hash functions: No hash function can guarantee, that no collisions will occur. They can only try to minimize the chance of collisions in typical workloads. One hash function which is often used is the bernstein hash function. You can find a comparison of different string hash functions (including bernstein) here
In C++ you can simply use the standard
map(see here) template which does not use a hash map, but is typically implemented using red-black trees. The C++11 standard has aunordered_map(see here) which is implemented using a hash function.