Hashing a string to an integer in c++ -
i trying figure out conversion process strings ints. doing program hashing, in key value hashed name of state. research, seems atoi() not work.
do need break each letter of word down , individually convert? use ascii? going in wrong direction?
i lost, information fantastic. thanks!
c++11 introduces implementation defined hashing function called std::hash
in header <functional>
has speciality string classes std::string
, std::wstring
, etc.
it's simple doing this:
#include <iostream> #include <functional> //for std::hash #include <string> int main() { std::string str = "hello world"; std::hash<std::string> hasher; auto hashed = hasher(str); //returns std::size_t std::cout << hashed << '\n'; //outputs 2146989006636459346 on machine }
specializing std::hash
user defined types isn't complex either. note there no std::hash
specialization const char*
or of c-strings.
Comments
Post a Comment