简体   繁体   English

stl中的优先级队列

[英]priority queue in stl

I followed the article to code a hanffman coding method using stl's priority_queue, however I think there are some bugs in the final code or it's not updated. 我按照文章学习了使用stl的priority_queue编写hanffman编码方法的方法,但是我认为最终代码中有一些错误,或者没有更新。 The main problem is the declaration of the priority_queue, I believe it should take three parameters like: priority_queue< node, vector, greater > q, instead of priority_queue, greater > q. 主要问题是priority_queue的声明,我认为它应该采用三个参数,例如:priority_queue <节点,向量,更大> q,而不是priority_queue,更大> q。 However, even with this changes, the gcc compiler still give the errors like: 但是,即使进行了此更改,gcc编译器仍然会给出如下错误:

  /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h: In member function ‘bool std::greater<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = node]’:
  /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_heap.h:279:   instantiated from ‘void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<node*, std::vector<node, std::allocator<node> > >, _Distance = long int, _Tp = node, _Compare = std::greater<node>]’
  /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_heap.h:404:   instantiated from ‘void std::make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<node*, std::vector<node, std::allocator<node> > >, _Compare = std::greater<node>]’
  /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_queue.h:367:   instantiated from ‘std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, const _Sequence&) [with _Tp = node, _Sequence = std::vector<node, std::allocator<node> >, _Compare = std::greater<node>]’ hanffman.cpp:119:   instantiated from here
  /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:218: error: no match for ‘operator>’ in ‘__x > __y’

which I don't understand. 我不明白。 The full code is available here 完整的代码在这里

You need to define an operator>() for your class because you are using std::greater<> as the comparison in the priority_queue . 您需要为类定义一个operator>() ,因为您使用std::greater<>作为priority_queue的比较。 The only operator I saw in the code was operator<() . 我在代码中看到的唯一运算符是operator<()

您缺少节点的>运算符的定义(仅定义<运算符)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM