Search code examples
c++data-structurespriority-queuedijkstra

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. 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. 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!


Solution

  • 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);