I'm trying to implement a priority queue right now, but I keep getting an error saying that push is not a member function. I never used priority queue before, so I'm pretty sure I'm messing up somewhere...
struct X
{
unordered_map<string, double> distance;
unordered_map<string, string> vertices;
};
void make(std::string source)
{
X distance;
X parent;
priority_queue<X, std::vector<X>, greater<void>> pq; //i'm pretty sure there's something wrong with the templates?
distance.distance[source] = 0;
pq.push(std::make_pair(start, 0));
The class has to be comparable and must respond to operators like < and > so that the queue can sort them:
class X {
public:
unordered_map<string, double> distance;
unordered_map<string, string> vertices;
int priority;
bool operator< (const X& b) {
return priority < b.priority;
}
bool operator> (const X& b) {
return priority > b.priority;
}
};
void make(std::string source) {
priority_queue<X, vector<X>, greater<void>> pq;
X element;
element.distance[source] = 0;
element.priority = 100;
pq.push(element);
}
The push expects (and accepts) parameter of type X, while you are passing something else (a pair).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With