This is NOT a duplicate question It differs here as the comparison function has a dependancy on the main class.
All my logic is in a class. I have a map nodeCnt which can be looked up upon getCnt() . I am figuring out for how to have a custom comparison myCmp for my priority queue pq which will do comparison based on the map.
The following snippet does not work. And having auto cmp as described in reference docs cannot be done as Class cannot have such declarations for comparison function.
Class AllLogic {
private:
std::map<int, int> nodeCnt;
// This is my confusing part
std::priority_queue<int, std::vector<int>, decltype(&myCmp)> pq(&myCmp);
public:
int getCnt(int val) {
if (nodeCnt.find(val) != nodeCnt.end())
return nodeCnt[val];
return 0;
}
bool myCmp(int left, int right) {
return getCnt(left) < getCnt(right);
}
};