使用自定义比较器声明 C++ 优先级队列的问题
Posted
技术标签:
【中文标题】使用自定义比较器声明 C++ 优先级队列的问题【英文标题】:Issues with Declaring a C++ Priority Queue using a custom Comparator 【发布时间】:2015-03-06 23:16:08 【问题描述】:我一直在尝试实现由自定义数据类型和自定义比较器组成的优先级队列,但是当我尝试编译时出现此错误。我尝试了多个声明,但它们都产生了这个错误。
priority_queue<myData, vector<myData>, myComp> myPQ;
priority_queue<myData, vector<myData>, myComp> myPQ(compVariable);
我也在尝试在创建这些优先级队列后创建它们。
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_function.h: In instantiation of ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Order]’:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_heap.h:183:47: required from ‘void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Order*, std::vector<Order, std::allocator<Order> > >; _Distance = long int; _Tp = Order; _Compare = std::less<Order>]’
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_heap.h:222:58: required from ‘void std::push_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Order*, std::vector<Order, std::allocator<Order> > >; _Compare = std::less<Order>]’
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_queue.h:499:41: required from ‘void std::priority_queue<_Tp, _Sequence, _Compare>::push(const value_type&) [with _Tp = Order; _Sequence = std::vector<Order, std::allocator<Order> >; _Compare = std::less<Order>; std::priority_queue<_Tp, _Sequence, _Compare>::value_type = Order]’
Market.cpp:144:32: required from here
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_function.h:235:20: error: no match for ‘operator<’ (operand types are ‘const Order’ and ‘const Order’)
return __x < __y;
^
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_function.h:235:20: note: candidates are:
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_algobase.h:64:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/char_traits.h:39,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_pair.h:220:5: note: template<class _T1, class _T2> constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
^
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_pair.h:220:5: note: template argument deduction/substitution failed:
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:48:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_function.h:235:20: note: ‘const Order’ is not derived from ‘const std::pair<_T1, _T2>’
return __x < __y;
^
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_algobase.h:67:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/char_traits.h:39,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_iterator.h:297:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
operator<(const reverse_iterator<_Iterator>& __x,
^
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_iterator.h:297:5: note: template argument deduction/substitution failed:
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:48:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_function.h:235:20: note: ‘const Order’ is not derived from ‘const std::reverse_iterator<_Iterator>’
return __x < __y;
^
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_algobase.h:67:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/char_traits.h:39,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_iterator.h:347:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
operator<(const reverse_iterator<_IteratorL>& __x,
^
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_iterator.h:347:5: note: template argument deduction/substitution failed:
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:48:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_function.h:235:20: note: ‘const Order’ is not derived from ‘const std::reverse_iterator<_Iterator>’
return __x < __y;
^
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_algobase.h:67:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/char_traits.h:39,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_iterator.h:1055:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::move_iterator<_Iterator>&, const std::move_iterator<_IteratorR>&)
operator<(const move_iterator<_IteratorL>& __x,
^
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_iterator.h:1055:5: note: template argument deduction/substitution failed:
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:48:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_function.h:235:20: note: ‘const Order’ is not derived from ‘const std::move_iterator<_Iterator>’
return __x < __y;
^
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_algobase.h:67:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/char_traits.h:39,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_iterator.h:1061:5: note: template<class _Iterator> bool std::operator<(const std::move_iterator<_Iterator>&, const std::move_iterator<_Iterator>&)
operator<(const move_iterator<_Iterator>& __x,
^
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_iterator.h:1061:5: note: template argument deduction/substitution failed:
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:48:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_function.h:235:20: note: ‘const Order’ is not derived from ‘const std::move_iterator<_Iterator>’
return __x < __y;
^
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:52:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/basic_string.h:2569:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/basic_string.h:2569:5: note: template argument deduction/substitution failed:
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:48:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_function.h:235:20: note: ‘const Order’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
return __x < __y;
^
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:52:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/basic_string.h:2581:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/basic_string.h:2581:5: note: template argument deduction/substitution failed:
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:48:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_function.h:235:20: note: ‘const Order’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
return __x < __y;
^
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:52:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/basic_string.h:2593:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator<(const _CharT* __lhs,
^
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/basic_string.h:2593:5: note: template argument deduction/substitution failed:
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:48:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_function.h:235:20: note: mismatched types ‘const _CharT*’ and ‘Order’
return __x < __y;
^
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/vector:64:0,
from P2.h:4,
from Market.cpp:3:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_vector.h:1420:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
^
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_vector.h:1420:5: note: template argument deduction/substitution failed:
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:48:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_function.h:235:20: note: ‘const Order’ is not derived from ‘const std::vector<_Tp, _Alloc>’
return __x < __y;
^
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/deque:64:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/queue:60,
from Market.cpp:6:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_deque.h:273:5: note: template<class _Tp, class _Ref, class _Ptr> bool std::operator<(const std::_Deque_iterator<_Tp, _Ref, _Ptr>&, const std::_Deque_iterator<_Tp, _Ref, _Ptr>&)
operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
^
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_deque.h:273:5: note: template argument deduction/substitution failed:
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:48:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_function.h:235:20: note: ‘const Order’ is not derived from ‘const std::_Deque_iterator<_Tp, _Ref, _Ptr>’
return __x < __y;
^
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/deque:64:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/queue:60,
from Market.cpp:6:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_deque.h:281:5: note: template<class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR> bool std::operator<(const std::_Deque_iterator<_Tp, _Ref, _Ptr>&, const std::_Deque_iterator<_Tp, _RefR, _PtrR>&)
operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
^
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_deque.h:281:5: note: template argument deduction/substitution failed:
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:48:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_function.h:235:20: note: ‘const Order’ is not derived from ‘const std::_Deque_iterator<_Tp, _Ref, _Ptr>’
return __x < __y;
^
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/deque:64:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/queue:60,
from Market.cpp:6:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_deque.h:1975:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::deque<_Tp, _Alloc>&, const std::deque<_Tp, _Alloc>&)
operator<(const deque<_Tp, _Alloc>& __x,
^
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_deque.h:1975:5: note: template argument deduction/substitution failed:
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:48:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_function.h:235:20: note: ‘const Order’ is not derived from ‘const std::deque<_Tp, _Alloc>’
return __x < __y;
^
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/queue:64:0,
from Market.cpp:6:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_queue.h:286:5: note: template<class _Tp, class _Seq> bool std::operator<(const std::queue<_Tp, _Seq>&, const std::queue<_Tp, _Seq>&)
operator<(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y)
^
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_queue.h:286:5: note: template argument deduction/substitution failed:
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:48:0,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from Market.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/stl_function.h:235:20: note: ‘const Order’ is not derived from ‘const std::queue<_Tp, _Seq>’
return __x < __y;
这是导致错误的示例:
class myData
public:
int a;
int b;
int c;
int d;
;
class myComp
public:
bool operator()(myData& d1, myData& d2)
if(d1.a == d2.a)
return d2.b < d2.c;
else
return d1.a < d2.a;
;
priority_queue<myData, vector<myData>, myComp> myPQ;
vector<priority_queue<myData, vector<myData>, myComp> > vec_PQ(n, myPQ);
【问题讨论】:
你确定你的“Order”类重载了“ 第一个错误看起来像是您使用了priority_queue<Order, std::vector<Order>>
类型而没有任何自定义比较器。请发布一个我们可以实际编译的MCVE。
@sithereal,使用自定义比较器的关键在于它不需要。
对不起,我的意思是“你确定你的“订单”类有“()”运算符重载吗?'
void std::priority_queue<_Tp, _Sequence, _Compare>::push(const value_type&) [with _Tp = Order; _Sequence = std::vector<Order, std::allocator<Order> >; _Compare = std::less<Order>; std::priority_queue<_Tp, _Sequence, _Compare>::value_type = Order]
...是的,绝对看起来像香草priority_queue<Order>
。
【参考方案1】:
发件人:http://en.cppreference.com/w/cpp/concept/Compare
比较概念是一些标准库工具对用户提供的函数对象类型所期望的一组要求。
应用于比较类型对象的函数调用操作的返回值,当上下文转换为
bool
时,如果调用的第一个参数出现在第二个之前,则生成true
严格的弱排序由此 Compare 类型引起的关系,否则为false
。与任何 BinaryPredicate 一样,不允许对该表达式的求值调用取消引用的迭代器的非常量成员函数。
myComp::operator()
的参数应该是 const&
。
bool myComp::operator()(myData const& d1, myData const& d2)
如@T.C. 所示。在注释中,函数本身应该是一个 const 成员函数,但在这种情况下,这不是必需的。
【讨论】:
而operator()
本身应该是const
。但这不是重点。以上是关于使用自定义比较器声明 C++ 优先级队列的问题的主要内容,如果未能解决你的问题,请参考以下文章