首页 > 解决方案 > 如何将字符串添加到杜鹃过滤器?

问题描述

我正在尝试使用来自 github 链接 https://github.com/efficient/cuckoofilter的布谷鸟过滤器代码

每当我使用数据类型字符串时,它都会给我错误 mycode:

cuckoofilter::CuckooFilter<string, 12> filter(total_items);

但每次我运行代码时都会出现这个错误

error: no match for call to ‘(const cuckoofilter::TwoIndependentMultiplyShift) (const std::_cxx11::basic_string&)’ 68 | const uint64_t hash = hasher(item);  

在下一行

https://github.com/efficient/cuckoofilter/blob/master/src/cuckoofilter.h#L68

标签: c++

解决方案


在您的过滤器声明中,您使用的是 std::string; 类型 期望 std::string 是 ItemType template <typename ItemType, ...> class CuckooFilter { }

但是,散列函数TwoIndependentMultiplyShift需要uint64_t类型键。该方法在文件https://github.com/efficient/cuckoofilter/blob/master/src/hashutil.h中。

由于类型不匹配,会发生运行时错误。在旧 C++ 标准下,这些类型在编译期间不受限制。因此,您看不到任何编译时错误。

要解决您的问题,您可能必须使用支持 std::string 的哈希函数或使用兼容的 ItemType。

C++20 引入了限制模板参数的概念,将有助于限制可用作模板参数的类型。有了这个错误将在编译时很明显。


推荐阅读