Have a good hash function for a C++ hash table?

前端 未结 9 1121
鱼传尺愫
鱼传尺愫 2020-12-12 18:15

I am in need of a performance-oriented hash function implementation in C++ for a hash table that I will be coding. I looked around already and only found questions asking wh

9条回答
  •  伪装坚强ぢ
    2020-12-12 18:51

    Since you store english words, most of your characters will be letters and there won't be much variation in the most significant two bits of your data. Besides of that I would keep it very simple, just using XOR. After all you're not looking for cryptographic strength but just for a reasonably even distribution. Something along these lines:

    size_t hash(const std::string &data) {
      size_t h(0);
      for (int i=0; i> 26) ^ data[i];
      }
      return h;
    }
    

    Besides of that, have you looked at std::tr1::hash as a hashing function and/or std::tr1::unordered_map as an implementation of a hash table? Using these would probably be save much work opposed to implementing your own classes.

提交回复
热议问题