priority_queue.top() 不返回正确的对象

priority_queue.top() does not return the right object

本文关键字:返回 对象 queue top priority      更新时间:2023-10-16

我正在尝试使用优先级队列来返回具有最低"计数"的对象"字节树"。因此,我在对象中实现了布尔运算符>函数。但这似乎不起作用,我根据其他东西获得对象

我尝试过将运算符

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 *>,但你在Bytetree上声明了operator >,而不是在Bytetree *上。