简体   繁体   English

如何在C ++中向std :: priority_queue添加键/优先级?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM