64,646
社区成员
发帖
与我相关
我的任务
分享
struct wordhash{
static const size_t bucket_size = 4;
static const size_t min_buckets = 8;
bool operator() (wchar_t *a,wchar_t* b)
{
return wcscmp(a,b)==0; //不允许同名,name为键值
} //将name换为city测试下
size_t operator()(wchar_t *str) const
{
if(!*str)
return 0;
register size_t hash = 5381;
while (size_t ch = (size_t)*str++)
{
hash = hash * 33 ^ ch;
}
return hash;
}
};
int main()
{
setlocale( LC_ALL, "chs" );
using namespace std;
hash_set<wchar_t *,wordhash> ihs;
wchar_t word_table[10][5]={0};
FILE *fp=fopen("user.txt","r");
int i=0;
while(!feof(fp))
{
fgetws(word_table[i],5,fp);
ihs.insert(word_table[i]);
i++;
}
cout<<"set 中的数据有"<<ihs.size()<<"个"<<endl;
hash_set <wchar_t *,wordhash> :: const_iterator ihs_AcIter, ihs_RcIter;
wchar_t word[5]={0};
wcscpy(word,L"牛逼");
ihs_RcIter = ihs.find(word);
if(ihs_RcIter==ihs.end())
cout<<"no words"<<endl;
else
cout<<"find it"<<endl;
system("pause");
return 0;
}