Below is the code snippet which is incorrect.
I want to implement dijkstra using priority queue stl of c++. But i cant figure out the right syntax for using this priority_queue with edge class. I want to put edge to priority_queue with based on weights.
class edge{
public:
int src;
int dest;
int wt;
};
class comp {
bool operator() (int a, int b) {
if(a<=b){
return true;
}else{
return false;
}
}
};
priority_queue<int,edge,comp> check;
edge e1,e2,e3;
e1.dest=1;
e2.dest=2;
e3.dest=3;
#include <queue>
#include <iostream>
class edge{
public:
int src;
int dest;
int wt;
};
struct less_weight {
bool operator() (const edge& a, const edge& b) {
return a.wt < b.wt;
}
};
struct greater_weight {
bool operator() (const edge& a, const edge& b) {
return a.wt > b.wt;
}
};
int main()
{
std::priority_queue<int,std::vector<edge>,less_weight> prioritise_higher_weights;
prioritise_higher_weights.push(edge{0, 1, 1 });
prioritise_higher_weights.push(edge{1, 2, 2 });
prioritise_higher_weights.push(edge{2, 3, 3 });
std::priority_queue<int,std::vector<edge>,greater_weight> prioritise_lower_weights;
prioritise_lower_weights.push(edge{0, 1, 1 });
prioritise_lower_weights.push(edge{1, 2, 2 });
prioritise_lower_weights.push(edge{2, 3, 3 });
std::cout << prioritise_higher_weights.top().wt << std::endl;
std::cout << prioritise_lower_weights.top().wt << std::endl;
}
expected output:
3
1
Note how priority queue orders by the inverse of the given predicate.