为什么hash_map.find参数必须是int

huis 2008-11-04 08:28:02
stdext::hash_map<_bstr_t,IndicationParam> indParams;
indParams.find(_bstr_t("aaa"));

编译时提示错误
错误 3 error C2440: “类型转换”: 无法从“const _bstr_t”转换为“size_t” d:\program files\microsoft visual studio 9.0\vc\include\xhash 71 GpTask

把_bstr_t改成int类型就可以!
...全文
327 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunxuehui 2008-11-04
  • 打赏
  • 举报
回复
http://www.cnblogs.com/huis/archive/2008/11/04/1326107.html
huis 2008-11-04
  • 打赏
  • 举报
回复
还是不行!
太乙 2008-11-04
  • 打赏
  • 举报
回复
~~~up~~~
e_sharp 2008-11-04
  • 打赏
  • 举报
回复
需要自己写hash函数,针对 _bstr_t 的特化版本

template<>
struct hash<_bstr_t> {...};
wangdeqie 2008-11-04
  • 打赏
  • 举报
回复

//看这个吧,偶又改了下
#include <cstdlib>
#include <iostream>
#include <string>
#include <hash_map.h>

using namespace std;

struct bstr_t_hash;
struct equal_A;

class bstr_t
{
friend struct bstr_t_hash;
friend struct equal_A;
public:
bstr_t(string s):str(s){}
private:
string str;
};

struct bstr_t_hash{
size_t operator()(const bstr_t& s) const
{
unsigned long __h = 0;
for (size_t i = 0 ; i < s.str.size() ; i ++)
__h = 5*__h + s.str[i];
return size_t(__h);
}
};
struct equal_A{
bool operator()(const class bstr_t & a1, const class bstr_t & a2)const{
return a1.str == a2.str;
}
};

int main(int argc, char *argv[])
{
hash_map <bstr_t,int,bstr_t_hash,equal_A> indParams;
indParams.find(bstr_t("aaa"));

system("PAUSE");
return EXIT_SUCCESS;
}

wangdeqie 2008-11-04
  • 打赏
  • 举报
回复

//比如这么写:运行环境 DEV C++
#include <cstdlib>
#include <iostream>
#include <hash_map.h>

using namespace std;

struct bstr_t_hash;

class bstr_t
{
friend struct bstr_t_hash;
public:
bstr_t(string s):str(s){}
private:
string str;
};

struct bstr_t_hash{
size_t operator()(const bstr_t& s) const
{
unsigned long __h = 0;
for (size_t i = 0 ; i < s.str.size() ; i ++)
__h = 5*__h + s.str[i];
return size_t(__h);
}
};

int main(int argc, char *argv[])
{
hash_map <string,int,bstr_t_hash> indParams;
indParams.find(("aaa"));

system("PAUSE");
return EXIT_SUCCESS;
}

lyle3 2008-11-04
  • 打赏
  • 举报
回复
学习
wangdeqie 2008-11-04
  • 打赏
  • 举报
回复
自定义类型,得重写哈希函数和比较函数
yeah920 2008-11-04
  • 打赏
  • 举报
回复
友情up一下.
帅得不敢出门 2008-11-04
  • 打赏
  • 举报
回复
10楼的方法 gcc编译通过
backway 2008-11-04
  • 打赏
  • 举报
回复
UP
wangdeqie 2008-11-04
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 huis 的回复:]
还是不行!
[/Quote]


//int是内建类型,是内部定义好的适用类型,所以可以直接用,而自定义类型包括string,都需要自己重写哈希函数和比较函数

//怎么会不行呢,我都运行出结果了,下面的运行结果:

find sucessful!
请按任意键继续. . .




//把下面的代码粘贴到 DEV C++下的一个新工程里:

#include <cstdlib>
#include <iostream>
#include <string>
#include <hash_map.h>

using namespace std;

struct bstr_t_hash;
struct equal_A;

class bstr_t //类
{
friend struct bstr_t_hash;
friend struct equal_A;
public:
bstr_t(string s):str(s){}
private:
string str;
};

struct bstr_t_hash //哈希函数
{
size_t operator()(const bstr_t& s) const
{
unsigned long __h = 0;
for (size_t i = 0 ; i < s.str.size() ; i ++)
__h = 5*__h + s.str[i];
return size_t(__h);
}
};
struct equal_A //比较函数
{
bool operator()(const class bstr_t & a1, const class bstr_t & a2)const{
return a1.str == a2.str;
}
};

int main(int argc, char *argv[])
{
hash_map <bstr_t,int,bstr_t_hash,equal_A> indParams;
indParams.insert(hash_map <bstr_t,int,bstr_t_hash,equal_A>::value_type(bstr_t("aaa"),1));
if(indParams.find(bstr_t("aaa"))!=indParams.end())
cout<<"find sucessful!"<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}
 本次课程会带着大家学习Hash算法,从源码的角度去学习算法,更加容易理解的方式去学习,能够更高效的吸收学到的内容,也能培养出能够独自看源码,分析源码的能力。Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值。这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,而不可能从散列值来唯一的确定输入值。简单的说就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数。  哈希表是根据设定的哈希函数H(key)和处理冲突方法将一组关键字映射到一个有限的地址区间上,并以关键字在地址区间中的象作为记录在表中的存储位置,这种表称为哈希表或散列,所得存储位置称为哈希地址或散列地址。作为线性数据结构与表格和队列等相比,哈希表无疑是查找速度比较快的一种。  通过将单向数学函数(有时称为“哈希算法”)应用到任意数量的数据所得到的固定大小的结果。如果输入数据中有变化,则哈希也会发生变化。哈希可用于许多操作,包括身份验证和数字签名。也称为“消息摘要”。  简单解释:哈希(Hash)算法,即散列函数。它是一种单向密码体制,即它是一个从明文到密文的不可逆的映射,只有加密过程,没有解密过程。同时,哈希函数可以将任意长度的输入经过变化以后得到固定长度的输出。哈希函数的这种单向特征和输出数据长度固定的特征使得它可以生成消息或者数据。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧