使用自定义哈希函数插入 unordered_set

2024-01-04

我有以下代码来制作unordered_set<Interval>。这编译得很好。

struct Interval {
  unsigned int begin;
  unsigned int end;
  bool updated;   //true if concat.  initially false
  int patternIndex;  //pattern index. valid for single pattern
  int proteinIndex;   //protein index.  for retrieving the pattern
};

struct Hash {
  size_t operator()(const Interval &interval);
};

size_t Hash::operator()(const Interval &interval){
  string temp = to_string(interval.begin) + to_string(interval.end) + to_string(interval.proteinIndex);
  return hash<string>()(temp);
}

unordered_set<Interval, string, Hash> test;

但是,当我尝试使用以下代码插入时,我无法编译:

for(list<Interval>::iterator i = concat.begin(); i != concat.end(); ++i){
  test.insert((*i));
}

而且,我无法从错误消息中确定问题所在,例如:

note: candidate is:
note: size_t Hash::operator()(const Interval&)
note:   candidate expects 1 argument, 2 provided  

我以为我只提供了 1 个参数...

我的插入代码有什么问题?


这是新的实例化代码:unordered_set<Interval, Hash> test;但是,我仍然收到大量错误消息,例如:

note: candidate is:
note: size_t Hash::operator()(const Interval&) <near match>
note:   no known conversion for implicit ‘this’ parameter from ‘const Hash*’ to ‘Hash*’

第一个问题:

你正在路过string作为实例化的第二个模板参数unordered_set<>类模板。第二个参数应该是哈希函数的类型 http://en.cppreference.com/w/cpp/container/unordered_set, and std::string不是可调用对象。

也许是想写:

unordered_set<Interval, /* string */ Hash> test;
//                      ^^^^^^^^^^^^
//                      Why this?

另外,我建议使用除begin and end为您的(成员)变量,因为这些是 C++ 标准库的算法名称。

第二个问题:

你应该记住,哈希函数应该被限定为const http://en.cppreference.com/w/cpp/container/unordered_set/hash_function,所以你的函子应该是:

struct Hash {
   size_t operator() (const Interval &interval) const {
   //                                           ^^^^^
   //                                           Don't forget this!
     string temp = to_string(interval.b) + 
                   to_string(interval.e) + 
                   to_string(interval.proteinIndex);
     return (temp.length());
   }
};

第三个问题:

最后,如果你想要的话std::unordered_set能够使用类型的对象Interval,您需要定义一个与您的哈希函数一致的相等运算符。默认情况下,如果不指定任何类型参数作为第三个参数std::unordered_set类模板,operator ==将会被使用。

您当前没有任何超载operator ==为你的班级Interval,所以你应该提供一个。例如:

inline bool operator == (Interval const& lhs, Interval const& rhs)
{
    return (lhs.b == rhs.b) && 
           (lhs.e == rhs.e) && 
           (lhs.proteinIndex == rhs.proteinIndex); 
}

结论:

经过上述所有修改后,您的代码将变为:

#include <string>
#include <unordered_set>
#include <list>

using namespace std;

struct Interval {
  unsigned int b;
  unsigned int e;
  bool updated;   //true if concat.  initially false
  int patternIndex;  //pattern index. valid for single pattern
  int proteinIndex;   //protein index.  for retrieving the pattern
};

bool operator == (Interval const& lhs, Interval const& rhs)
{
    return (lhs.b == rhs.b) && (lhs.e == rhs.e) && (lhs.proteinIndex == rhs.proteinIndex); 
}

struct Hash {
   size_t operator()(const Interval &interval) const {
     string temp = to_string(interval.b) + to_string(interval.e) + to_string(interval.proteinIndex);
     return (temp.length());
   }
};

int main()
{
   unordered_set<Interval, Hash> test;
  
  list<Interval> concat;
  for(list<Interval>::iterator i = concat.begin(); i != concat.end(); ++i){
    test.insert(*i);
  }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用自定义哈希函数插入 unordered_set 的相关文章

随机推荐

  • Crontab 突然停止在服务器上工作?

    我在 Linux 平台上的服务器上设置了一些 crontab 在那 2 天之前 所有 cron 都在运行 我不知道 crontab 发生了什么 它们现在不工作 所有 cron 之前都在运行 之后我添加了一个新的 crontab 它们没有运行
  • HttpWebRequest 对象的诊断转储

    有没有什么好方法 除了繁琐地查询每个属性之外 在 C 中为 HttpWebRequest 构建诊断转储字符串 对于简单的对象 人们可以使用new JObject theObject 但这不适用于 HttpWebRequest 并且toStr
  • 使用 url 创建 yii2 动态页面:www.example.com/pageName

    在我的系统中 用户需要有他们的个人资料页面 我要求这些页面将显示在 url 中 如下所示 www example com John Doe www example com Mary Smith 如何在 yii2 中实现这些 URL 这些 J
  • 使用VBA获取word中的所有交叉引用

    我有一个相当大的 Word 文档 gt 400 页 其中有很多标题的交叉引用 到目前为止 我一直引用标题的标题 但现在我想更改它并引用标题所在的页面 我没有通过 GUI 找到解决方案 当然 手动处理除外 所以我正在考虑编写一些 VBA 不幸
  • XPATH获取text_1和text_2之间的所有节点

    我有这样的 HTML div text1 a link 1 a a link 2 a text2 a link 3 a text3 div 我想获取之间的所有节点text1 and text2 问题是没有p or span标签 只有纯文本
  • Google App Engine 在处理信号后终止:术语

    我有一个应用程序部署为 GCP 上的应用程序引擎 它调用 API 创建 Bytes IO 流并将数据上传到云存储 有 4 个文件 大约 44mb 需要上传 文件是 URL 列表 然后将其提供给数据流 但这并不重要 一开始 我为每个文件创建了
  • 带有负秒的奇怪 mktime 逻辑

    我一直在使用 mktime localtime 进行时间管理 包括一些关于日期 时间的繁重算术 当向 mktime 提供包含负值的 struct tm 时 我注意到一些非常奇怪的事情 采取下面的代码 2013 年 11 月 3 日 洛杉矶的
  • 如何在C中分配和释放对齐内存

    如何分配与 C 中特定边界 例如缓存行边界 对齐的内存 我正在寻找类似 malloc free 的实现 理想情况下应尽可能可移植 至少在 32 位和 64 位架构之间 编辑添加 换句话说 我正在寻找一些行为类似的东西 现在已经过时了 mem
  • 在 R / Rmarkdown 中同步两个传单地图

    JS 传单允许需要同步的两个地图 https github com turban Leaflet Sync 查看同步传单地图示例here http blog thematicmapping org 2013 06 creating sync
  • 在同时活动的多个进程中更新 Java 中的数据

    我正在创建一个带有传感器的令牌环 其中每个传感器都是一个独立的进程 当我启动传感器时 它与网关通信并获取系统上已有的实际传感器的列表 问题是 每次我开始一个新进程时 我希望每个现有的传感器都能获取更新的列表 以便了解其他传感器已被添加 并且
  • 如何制作无限分页的UICollectionView?

    我有一个包含 6 页且启用分页的 UICollectionView 和一个 UIPageControl 我想要的是 当我来到最后一页时 如果我向右拖动 UICollectionView 会从第一页无缝地重新加载 void scrollVie
  • VS 2017 的 .net Core Webpack 发布失败

    我正在尝试将 net Core Angular 4 项目从 Visual Studio 发布到 Azure 在发布过程中 我收到以下错误 ERROR in gendir angular tree component dist compone
  • 求和与分组

    我有一个如下所示的数据框 全部控股基金 BrokerBestRate notional current DistanceBestRate 0 CITI 7 859426e 05 0 023194 1 WFPBS 3 609674e 06 0
  • 如何避免 Observable 中超出最大调用堆栈大小?

    我已经过滤了列表 public filteredEvents new BehaviorSubject 以及反转属性的方法checked export并将更改推回 public checkAll void this filteredEvent
  • Qt 5.5 - 触摸屏事件仅在初始(第一个)窗口中工作

    我已经设置了一个基本的 Qt Widgets Application Qt 5 5 社区 其中包含一个简单的 QWidget MainWindow 和一个附加的 QWidget SettingsScreen 在 MainWindow 中 触
  • Django - 与模型无关的自定义管理页面

    我正在使用 Django 1 7 和 Mezzanine 我希望在管理中有一些页面 工作人员可以在其中使用按钮和其他控制元素调用一些操作 管理命令等 我还想避免创建新模型 或手动创建模板并添加指向它的链接 如果可能 实现这一目标的最常见 最
  • 在 pthread_mutex_init 之前调用 pthread_mutex_lock 是否安全?

    我以前从未有机会使用 pthreads 库 但我正在审查一些涉及 pthread 互斥体的代码 我检查了文档pthread mutex lock and pthread mutex init 通过阅读这两个函数的手册页 我的理解是我必须调用
  • ES6 模块和继承

    我有以下 JavaScript 文件 src js classes Lexus js import Car from src js classes Car export class Lexus extends Car constructor
  • 两个 .NET 线程和硬件访问的问题

    我正在创建一个通过 FT2232H USB RS232 转换器与设备通信的应用程序 为了进行通信 我使用 FTDI 网站上的 FTD2XX NET dll 库 我正在使用两个线程 第一个线程不断从设备读取数据 第二个线程是Windows窗体
  • 使用自定义哈希函数插入 unordered_set

    我有以下代码来制作unordered set