C++ 插入<;int,pair<;int,int>&燃气轮机;优先级队列

C++ 插入<;int,pair<;int,int>&燃气轮机;优先级队列,c++,heap,max-heap,C++,Heap,Max Heap,好吧,我猜一下,因为你还没有解释你自己 我假设您希望将对放入优先级队列,并使用代码中的这些公式对它们进行排序 no instance of overloaded function "std::priority_queue<_Tp, _Sequence, _Compare>::push [with _Tp=std::pair<int, int>, _Sequence=std::vector<std::pair<int, int>, std::al

好吧,我猜一下,因为你还没有解释你自己

我假设您希望将对放入优先级队列,并使用代码中的这些公式对它们进行排序

no instance of overloaded function "std::priority_queue<_Tp, _Sequence, _Compare>::push [with _Tp=std::pair<int, int>, _Sequence=std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>, _Compare=int]" matches the argument list -- argument types are: (int, std::pair<int, int>) -- object type is: std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>, int>
最简单的方法是使用lambda函数,使用上面的公式比较两个对。下面是一些示例代码

int x1 =  a[i][0]*a[i][0];
int y1 =  a[i][1]*a[i][1]; 
    
int sum = x1 + y1;
#包括
#包括
使用名称空间std;
int main()
{
自动λ=[](对x,对y){
返回x.first*x.first+x.second*x.second<
y、 第一个*y.first+y.second*y.second;};
优先级队列最大值;
int a[3][2]={{1,2},{2,2},{1,0};
int n=3;

对于(int i=0;iI不明白-您想如何将
int,std::pair
推入包含
元素的容器中?priority\u queue maxh;std::priority\u queue模板的第二个参数是容器。请检查此处的定义,您应该尝试并解释您试图实现的目标,因为您的代码太混乱了,它是n很明显。例如这里
maxh.push(求和,生成对(a[i][0],a[i][1]))
您试图将两件事推送到队列中,但一次只能将一件事推送到优先级队列中。而且队列不可能同时包含整数和对。如果我不得不猜测,我会说您试图将对放到优先级队列中,并按
求和
排序。如果是这种情况,那么这不是正确的方法。我的第一直觉是使用
std::priority_queue
push({sum,{a[i][0],a[i][1]})
,让实现处理其余部分。当然,你的更好,占用的内存比我的更少!:)@brc dd我没有想到这种方法。这很可能是OP想要写的。这两种方法似乎相当。
no instance of overloaded function "std::priority_queue<_Tp, _Sequence, _Compare>::push [with _Tp=std::pair<int, int>, _Sequence=std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>, _Compare=int]" matches the argument list -- argument types are: (int, std::pair<int, int>) -- object type is: std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>, int>
int x1 =  a[i][0]*a[i][0];
int y1 =  a[i][1]*a[i][1]; 
    
int sum = x1 + y1;
#include <queue>
#include <utility>
using namespace std;

int main ()
{
    auto lambda = [](pair<int, int> x, pair<int, int> y){ 
        return x.first*x.first + x.second*x.second < 
               y.first*y.first + y.second*y.second; };
    priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(lambda)> maxh;
    int a[3][2] = { {1,2}, {2,2}, {1,0} };
    int n = 3;
    for (int i=0; i<n; i++) {
        maxh.push(make_pair(a[i][0],a[i][1]));
    }
    return 0;
}