我正在尝试使用优先级队列返回具有最低“计数”的对象“Bytetree”。因此我在我的对象中实现了一个 bool operator > 函数。但这似乎不起作用,我根据其他东西获得对象
我已经尝试将 operator< 作为友元函数或成员函数来实现,并进行了无数次修改,但它似乎根本没有被调用过。为什么不呢?
class Bytetree{
public:
bool leaf;
unsigned char byt;
int count;
Bytetree* child0;
Bytetree* child1;
Bytetree(unsigned char c): byt(c), count(0), child0(nullptr), child1(nullptr), leaf(true){};
Bytetree(Bytetree* c0, Bytetree* c1): child0(c0), child1(c1), count(c0->count+c1->count), leaf(false){};
bool operator>(const Bytetree & right) {
std::cout << "called at all" ;
return count > right.count;
}
[...]
}
主要
...
std::priority_queue<Bytetree*, std::deque<Bytetree*>, std::greater<Bytetree*> > que;
for (int i = 0; i<WORDLENGTH; i++){
que.push(mywc.wordar[i]);
// mywc.wordar[i]->print();
}
while(que.size()>=2){
Bytetree* bt0= que.top();
que.pop();
Bytetree* bt1= que.top();
que.pop();
que.push(new Bytetree(bt0, bt1));
}
最佳答案
您已经使用了std::greater<Bytetree *>
, 但你已经声明了 operator >
在 Bytetree
, 不在 Bytetree *
.
关于c++ - priority_queue.top() 没有返回正确的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56384874/