c++stringhashtablestring-hashing

How do I hash strings in C++?


I'm currently learning about hash table. Hashing integers are easy, but my assignment is to hash strings. I have given strings:

25674316-6058714                
56105665-7450612                
96917015-1417157                
48189873-3313151    

I have to hash them to fit into array of buckets[4]. How do I hash strings?


Solution

  • With the standard libraries hash function:

    std::string stringToHash = "25674316-6058714";
    size_t result = std::hash<std::string>()(stringToHash);