EE_DSnP 板


LINE

给一个 hash 的例子: class A { public: A(int d = 0):_data(d) {} size_t operator() () const { return _data; } bool operator == (const A& k) const { return _data == k._data; } private: int _data; }; int main() { Hash<A, double> hh(7); // initialize #buckets = 7 hh.insert(10, 10.0); // key = A(10), data = 10.0 hh.insert(20, 20.0); hh.insert(10, 15.0); hh.insert(2, 2.0); hh.insert(5, 5.0); hh.insert(16, 16.0); cout << "Hash..." << endl; Hash<A, double>::iterator it; it = hh.begin(); for (; it != hh.end(); ++it) cout << (*it).first() << " --> " << (*it).second << endl; Cache<A, double> cc(7); cc.write(10, 10.0); cc.write(20, 20.0); cc.write(10, 15.0); cc.write(2, 2.0); cc.write(5, 5.0); cc.write(16, 16.0); cout << "Cache..." << endl; for (size_t i = 0, n = cc.size(); i < n; ++i) cout << cc[i].first() << " --> " << cc[i].second << endl; } ※ 引述《BBSealion (海狮)》之铭言: : 我想请问一下有关 : HashKey的constructor : 和size_t operator() () const; : 的问题... : HashKey照名字来说 : 要造出一个hash用的key : 而这个key应该要和要丢到hash的data有关,所以必须有地方input data才行 : 我原本想法是写在constructor里面 : HashKey(AIGgate data) {num = f(data)}; : f()是我的hash function : num 是造出来的key number : 但老师预设的Hash class中,写了: : size_t bucketNum(const HashKey& k) const { : return (k() % _numBuckets); } : 所以这个bucketNum 要把一个key丢进去,然後用operator () 造出一个 number : 所以这个operator的目的比较像是:再将key转换成可以用的数字,用来数地几个bucket : (因为key可能还不是数字?) : 在此operator ()感觉上 比较不像个function的用法 : 因为function感觉上就是要 a = f(b)比较像个function吧 : --- : 简单说我的结论是 我真正的转换function 用data生出key,应该要自己写一个 f() : 而operator () 是拿来真正把key变成第几个bucket的数字用 : --- : 但投影片上又写说 : size_t operator() () const; // acted as “hash function” : 跟我想像的还是有点冲突 : 所以应该是怎麽样才对呢?? HashKey 里头的 data member 可以是任何你想要用来产生 hash key 的 data, 像是上面的 class A 里头只有一个 int, 但它也可以像是: class B { private: string _str; }; 或是: class C { private: size_t _d0; size_t _d1; bool _f; }; 至於 size_t operator () () { ... } 是希望用 HashKey 里头的 data memeber 算出一个 size_t 的值出来,它的脚色就像是 hash function 一样,比方说: size_t A::operator() () { return _data; } size_t B::operator() () { return (17033 + (_str.empty()? 29017: _str[0] <<3) + ((_str.size() <= 1)? 65521: _size[1] << 11)); } size_t C::operator() () { return ((_d0 << 3) + (_d1 << 5) + (_f? 1: 0)); } 请注意,由於 HashKey 不应该知道 #buckets 是多少, 也应该要应用到不同 #bucket 的 hash, 所以 operator() 只 return 一个 size_t, 最後才交由 Hash 里头的 buckNum() 算出 bucket index: size_t bucketNum(const HashKey& k) const { return (k() % _numBuckets); } : --- : PS: 另外,感觉上这次hash function的限制特别多 : 我一定要把它所有功能都做完吗?? : 还是我可以任意删减或增加function 只要达到strash的目的就好呢? Uh... 你们可以先写到让 strash 可以动作就好, 但我们也会测一两个使用 hash 的 testcases, 来确定大家的 hash implement 是否正确。 但占分应该很低,所以大家可以先不用写那麽多! --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.36.54.155







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Boy-Girl站内搜寻

TOP