приоритетная очередь в stl

#c #stl #priority-queue

#c #stl #приоритетная очередь

Вопрос:

Я следил за статьей, чтобы закодировать метод кодирования hanffman, используя priority_queue stl, однако я думаю, что в конечном коде есть некоторые ошибки или он не обновляется. Основная проблема заключается в объявлении priority_queue, я считаю, что он должен принимать три параметра, например: priority_queue< узел, вектор, больше> q , вместо priority_queue, больше> q. Однако, даже с учетом этих изменений, компилятор 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 _Tpamp;, const _Tpamp;) 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 _Compareamp;, const _Sequenceamp;) [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 foroperator>’ in ‘__x > __y’
  

чего я не понимаю. Полный код доступен здесь

Ответ №1:

Вам необходимо определить an operator>() для вашего класса, потому что вы используете std::greater<> в качестве сравнения в priority_queue . Единственный оператор, который я видел в коде, был operator<() .

Ответ №2:

Вам не хватает определения > оператора для узла (вы определяете только < оператор).