I have numbers from 1 to n, and an array of priorities P of size n.
I want to declare a priority queue using P as their priorities.
I don't want to define a new type of object that contains the number and its priority, I want to use a priority queue of int objects and pass a custom comparison object that depends on P to std::priority_queue. I tried the following but it doesn't work:
std::priority_queue<int, vector<int>, [P](int i, int j){ return P[i]<P[j]; }> PQ;
I also tried defining a class with a bool operator(int i, int j) {P[i] < P[j]} member and a constructor where I can pass P to it but that also didn't work.
How should I declare my priority queue?
The following seems to work:
class MyLess{
public:
MyLess(const int* p) {p_ = p;}
bool operator()(const int &lhs, const int &rhs) const { return p_[lhs] < p_[rhs]; }
private:
const int* p_;
};
...
std::priority_queue<int, vector<int>, MyLess> PQ(P);