c++ - priority_queue.top() 没有返回正确的对象

标签 c++

我正在尝试使用优先级队列返回具有最低“计数”的对象“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/

相关文章:

java - C++ Thrift : Can I use TFramedTransportFactory with TThreadedServer?

c++ - 在 C++ 中使用函数或类来完成简单任务?

c++ - 在我的数组中获取内存地址?

c++ - 嵌入式 C++ : to use STL or not?

c++ - 通过构造函数初始化类内部的指针

c++ - Linux 中的 sprintf uint64_t

c++ - 多个按钮的插槽

c++ - vector 下标超出范围错误 - C++ Vectors & Open CV

android - 使用 NDK cmake 链接静态库 libm.a 或 libc.a

c++ - 获取基类指针的派生类