这不是一个重复的问题它与此处的不同之处在于比较函数对主类有依赖性。如何在C++中实现STL priority_queue中的这种比较类
我所有的逻辑都属于一个类。我有一张地图nodeCnt
,可以查询getCnt()
。我正在计算如何为我的优先队列pq
定制比较myCmp
,该比较将根据地图进行比较。
以下代码段不起作用。如reference docs中所述的auto cmp
不能完成,因为Class不能具有用于比较功能的这种声明。
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);
}
};
Hi @kfsone。这不重复。这与现有的“略有不同”。 – visweshn92
比较函数对主类有依赖性。与您发布的相关重复链接不同。 – visweshn92
不要在stackoverflow中说“不起作用”,而只是描述问题...... – 2016-10-01 21:41:02