如何在比较功能中使用unordered

编程入门 行业动态 更新时间:2024-10-08 20:29:52
本文介绍了如何在比较功能中使用unordered_set?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我为std :: unorderd_set的第三个模板参数编写了自己的比较函数。 我的功能是

I wrote my own compare function for the third template parameter of std::unorderd_set. My function is

static bool HasSamePosition(const Node& a, const Node& b);

。现在,我尝试在我的无序集中使用此功能,

in the class Node. Now I'm trying to use this function in my unordered set,

std::unordered_set<Node, std::hash<Node>, bool(*)(const Node& a, const Node& b)> closedlist(&Node::HasSamePosition);

,但是它不起作用。错误是,没有构造函数的实例与参数列表匹配。我缺少什么?

but it doesn't work. The error ist, that no instance of the constructor is matching the argumentlist. What am I missing?

推荐答案

编译器正确。没有构造函数只允许您传递 KeyEqual 作为参数。您需要使用其他构造函数(请参见此处)或更改您的功能。

Well the compiler is right. There is no constructor that allows you to only pass KeyEqual as parameter. You need to use another constructor (see here) or change the type of your function.

例如您可以使用包装在HasSamePosition调用周围的辅助结构,并覆盖 operator()(const Node& a,const Node& b)

E.g. You could use a helper struct that wraps around your HasSamePosition call and override operator()(const Node& a, const Node& b)

struct Node{ static bool HasSamePosition(const Node& a, const Node& b); }; struct NodeEqual { bool operator()(const Node& a, const Node& b) { return Node::HasSamePosition(a, b); } }; int main() { std::unordered_set<Node, std::hash<Node>, NodeEqual> closedlist(); }

更多推荐

如何在比较功能中使用unordered

本文发布于:2023-11-29 01:53:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644824.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能   如何在   unordered

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!