priority_queue.top() 不返回正确的对象
priority_queue.top() does not return the right object
我正在尝试使用优先级队列来返回具有最低"计数"的对象"字节树"。因此,我在对象中实现了布尔运算符>函数。但这似乎不起作用,我根据其他东西获得对象
我尝试过将运算符
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 *
上。
相关文章:
- 如何返回对象C++的数组
- 如何使用std::min和std::less返回对象
- 如何通过引用返回对象
- 如何安全地从 DLL 调用返回对象
- 如何在不销毁对象的情况下返回对象列表
- 什么更好?返回对象指针列表?或返回指向对象列表的指针?
- 使用返回对象的函数处理错误
- 何时返回指针与返回对象的一般经验法则?
- 如何使用运算符+重载函数正确返回对象?
- C++,class,一个返回对象的函数
- 运算符的返回对象 = C++ 中的重载
- 从队列返回对象的最快方法,但前提是队列有它
- 如何使用条件表达式返回对象指针?
- 使用构造函数从函数返回对象
- 隐式转换函数的返回对象时是否会影响性能?
- 返回对象时从'const DList<int>* const'到 'DList<int>*' [-fallowive] 的转换无效
- 按值和堆栈大小限制返回对象
- 我应该如何从函数返回对象?
- c++:何时传递指针与返回对象
- 返回对象会丢失属性