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

时间:2019-05-30 20:00:08

标签: c++

我正在尝试使用优先级队列来返回“计数”最低的对象“字节树”。因此,我在对象中实现了布尔运算符>函数。但这似乎不起作用,我得到的是基于其他物体的物体

我曾尝试将操作符<作为朋友或成员函数并进行了无数次修改,但似乎根本没有被调用过。为什么不呢?

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));
}

1 个答案:

答案 0 :(得分:1)

您已使用std::greater<Bytetree *>,但已在operator >而非Bytetree上声明了Bytetree *

相关问题