C ++ hash function for string in int

I am looking for a hash function in C ++ for hashing a string for an int. I used CMapStringToPtr, but it has a function called GetNextAssoc that allows you to retrieve the key as a string, which means that the string needs to be saved and it gets so much memory. Is there another hash function that gets less memory and doesn't save the string?

+4
source share
2 answers

C ++ has a built-in hash function for this purpose - it is used for all containers of the STL hash.

std::hash

PS: you can also make your own, just pass the string through the const link and cycle through its characters one by one, adding them to an integer, then change by some value :)

+9
source
  int hash( const string &key, int tableSize) { int hashVal = 0; for(int i = 0; i<key.length(); i++) hashVal = 37*hashVal+key[i]; hashVal %= tableSize; if(hashVal<0) hashVal += tableSize; return hashVal; } 
+2
source

Source: https://habr.com/ru/post/1387094/


All Articles