c-尝试使用该类的结构中的变量使自定义类的优先级队列
作者:互联网
所以这是我的课程,目标是使bnode的优先级队列顺序排列,以使具有最低计数符号的bnode具有最高优先级.这是我的代码:
struct symbol {
explicit symbol(char av = 0, int ac = 0) : value(av), count(ac) { }
char value; // actual symbol, by default 0 (empty)
int count; // count of the symbol, by default 0
}; // symbol
// compare two symbols
// symbol with a lower count is "less than" symbol with a higher count
inline bool operator<(const symbol& lhs, const symbol& rhs) {
return ((lhs.count < rhs.count) || (!(rhs.count < lhs.count) && (lhs.value < rhs.value)));
} // operator<
template <typename T> struct bnode {
explicit bnode(const T& t = T(), bnode* l = 0, bnode* r = 0)
: value(t), left(l), right(r) { }
T value; // payload
bnode* left; // left child
bnode* right; // right child
}; // struct bnode
#endif // SYMBOL_HPP
//and here is me trying to make a priority queue:
std::priority_queue<bnode<symbol>,std::vector<bnode<symbol> >, std::less<std::vector<bnode<symbol> >::value_type::value> > queue;
这导致错误:错误:’bnode :: value’不能出现在常量表达式中
解决方法:
我认为您想要这样:
std::priority_queue<bnode<symbol>, std::vector<bnode<symbol> >,
std::less<std::vector<bnode<symbol> >::value_type> > queue;
标签:c,c11,priority-queue,queue 来源: https://codeday.me/bug/20191012/1903431.html