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

前端 未结 10 1755
长发绾君心
长发绾君心 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:23

    You can make use of the member functions operator[] or at of the string class or iterators to access individual char of a string object without converting it to c-style char array.

    To hash a string object to an integer you'll have to access each individual char of the string object which you can do as:

    for (i=0; i < str.length(); i++) {
        // use str[i] or str.at(i) to access ith element.
    }
    

提交回复
热议问题