#include<iostream>
#include<string>
#include<fstream>
using namespace std;
unsigned int ELFHash(string s)
{
unsigned int hash=0;
unsigned int x=0;
int i=0;
while(i<s.length())
{
hash=(hash<<4)+(s[i++]);
if((x=hash&0xF0000000L)!=0)
{
hash^=(x>>24);
hash&=~x;
}
}
return (hash&0x7FFFFFFF);
}
int main()
{
string s="jun";
//char *a="wang";
cout<<ELFHash(s);
return 0;
}
http://www.360doc.com/content/10/1221/23/4850421_80240551.shtml这里还有其他的哈希函数,以字符串为关键字的都可以用。像上面那样把string 改成char*类型就0k了。
原文链接: https://www.cnblogs.com/wjhkxcz/archive/2013/03/28/2986624.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/82380
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!