1. You use C++ strings but did not #include <string>.
2. As pointed out earlier, you should pass key.c_str() to hash().
3. h is uninitialised in hash().
I think that if you are going to alter your original code anyway, then it makes sense to just change it to use C++ strings, e.g.,
This way, you can just call hash(key) in your main function.Code:unsigned int hash(const std::string& key)
{
unsigned int h = 0;
const unsigned char* p = reinterpret_cast<const unsigned char*>(key.c_str());
int len = key.length();
for(int i=0; i<len; i++)
{
h = 33 * h + p[i];
}
return h;
}