How can I hash a string to an int using c++?

前端 未结 10 1752
长发绾君心
长发绾君心 2020-12-29 07:27

I have to write my own hash function. If I wanted to just make the simple hash function that maps each letter in the string to a numerical value (i.e. a=1, b=2, c=3, ...), i

10条回答
  •  隐瞒了意图╮
    2020-12-29 08:03

    Another way for small strings:

    int hash(const char* str) {
        int hash = 0;
        int c = 0;
    
        while (c < std::strlen(str)) {
            hash += (int)str[c] << (int)str[c+1];
            c++;
        }
        return hash;
    }
    

提交回复
热议问题