[英]How to add a key/ priority to std::priority_queue in c++?
I'm trying to implement Dijkstra's Pathfinding Algorithm using the std::priority_queue
. 我正在尝试使用
std::priority_queue
实现Dijkstra的寻路算法。 My Queue is of type Node*
and I need it to prioritize based on a float gScore
stored inside Node
from smallest gScore to largest. 我的队列是
Node*
类型的,我需要它根据存储在Node
从最小gScore到最大gscore的float gScore
进行优先级排序。 I've read the documentation but I still don't understand how this can be achieved. 我已经阅读了文档,但仍然不知道如何实现。 Any ideas?
有任何想法吗?
I don't understand what the type means by container_type(vector) 我不明白容器_类型(矢量)的含义
std::priority_queue<Node*> queue;
I greatly appreciate any help! 我非常感谢您的帮助!
You can create a class which will overload ()
您可以创建一个将重载
()
class cmp
{
public:
bool operator()(const Node *a, const Node *b) const
{
return (a->gscore) > (b->gscore);
}
};
Then 然后
std::priority_queue<Node*,std::vector<Node*>,cmp);
You will need a comparator function. 您将需要一个比较器功能。 I think it would be better to implement in a manner given below , rather than declaring a class for it.
我认为最好以下面给出的方式实现,而不是为此声明一个类。
auto comp = [] (Node* a,Node* b) -> bool { return a->gscore < b->gscore; };
priority_queue< Node*, std::vector<Node*>, decltype(comp) > foo(comp);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.