C++_字符串哈希函数(字符串也可以加下标,依次提取每个字符)
#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了。