I want to implement Dijkstra algorithm and in serious need for storing vertex in a queue .
#include <iostream>
#include <queue>
using namespace std;
int main ()
{
priority_queue<int> mypq;//I want to put a pointer to vertex instead of int
mypq.push(10);//Here I want to push vertex
mypq.push(20);
mypq.push(15);
cout << "mypq.top() is now " << mypq.top() << endl;
return 0;
}
Read the commented section.
The main thing to keep in mind is that a
priority_queueis a sorted container, so it requires that you define a comparison for the objects being stored (which must follow a strict, weak ordering).Since you talk about Dijkstra’s algorithm, let’s assume each vertex has a weight, and we want the vertices ordered by those weights.
Now a priority_queue of vertex objects is pretty easy:
Edit: You’ll need to define an insertion operator for that last line to work — something like: