[英]Type error in C++ when instantiating a priority_queue of int pairs with a custom comparator (to implement a min heap)
Currently working through a leetcode problem for which I need a min Heap of pairs.目前正在解决我需要最小堆对的 leetcode 问题。
I am trying to use a priority_queue
with int
pair
s and a custom compare type.我正在尝试将
priority_queue
与int
pair
和自定义比较类型一起使用。
Attempts of implementation of the same have failed with the following error:执行相同的尝试失败并出现以下错误:
In file included from prog_joined.cpp:1: In file included from./precompiled/headers.h:55: In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/queue:64: /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_queue.h:467:7: error: static_assert failed due to requirement 'is_same<int, std::pair<int, int>>::value' "value_type must be the same as the underlying container" static_assert(is_same<_Tp, typename _Sequence::value_type>::value, ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 9: Char 67: note: in instantiation of template class 'std::priority_queue<int, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>, std::greater>' requested here priority_queue<int, vector<pair<int, int>>, greater> pq;
在 prog_joined.cpp:1 中包含的文件中:在 /precompiled/headers.h:55 中包含的文件中:在 /usr/bin/../lib/gcc/x86_64-linux-gnu/9/.. 中包含的文件中/../../../include/c++/9/queue:64: /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../。 ./include/c++/9/bits/stl_queue.h:467:7: error: static_assert 由于要求'is_same<int, std::pair<int, int>>::value' "value_type 必须相同而失败作为底层容器” static_assert(is_same<_Tp, typename _Sequence::value_type>::value, ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~ 第 9 行:字符 67:注意:在模板 class 'std::priority_queue<int, std::vector<std 的实例化中::pair<int, int>, std::allocator<std::pair<int, int>>>, std::greater>' 在此请求 priority_queue<int, vector<pair<int, int>>, Greater> pq;
Here's the code with which I'm trying to implement the heap:这是我试图实现堆的代码:
#include <queue>
#include <utility>
using namespace std;
//...
priority_queue<int, pair<int, int>, greater<int>> pq;
As you can see in the std::priority_queue
documentation :正如您在
std::priority_queue
文档中看到的:
pair<int,int>
in your case).pair<int,int>
)。 For example if you want a priority_queue
of int
pair
s that use std::vector
as a container, you need:例如,如果您想要一个使用
std::vector
作为容器的int
pair
的priority_queue
,您需要:
std::priority_queue<std::pair<int, int>, // data type
std::vector<std::pair<int,int>>, // container type
std::greater<std::pair<int,int>>> pq; // compare type
Note: as you can see here the std::pair
implementation for comparison operators which are used by std::greater
is:注意:正如您在此处看到的,
std::greater
使用的比较运算符的std::pair
实现是:
Compares lhs and rhs lexicographically by operator<, that is, compares the first elements and only if they are equivalent, compares the second elements.
通过 operator< 按字典顺序比较 lhs 和 rhs,即比较第一个元素,只有当它们相等时,才比较第二个元素。
If this is not what you need you can implement your own compare type for std::pair<int,int>
.如果这不是您需要的,您可以为
std::pair<int,int>
实现自己的比较类型。
A side note: it is better to avoid using namespace std
.附注:最好避免
using namespace std
。 See here: Why is "using namespace std;"见这里: 为什么“使用命名空间标准;” considered bad practice?
被认为是不好的做法? .
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.