-5
priority_queue<pair<int,int>,int> maxh;

error:no default constructor exists for class "std::priority_queue<std::pair<int, int>, int, <error-type>>"

If i try this method

priority_queue<pair<int,int>,vector<pair<int,int>>,int> maxh;
    for (int i=0;i<n,i++){
        
        int x1 =  a[i][0]*a[i][0];
        int y1 =  a[i][1]*a[i][1]; 
        
         int sum = x1 + y1;
        maxh.push(sum,make_pair(a[i][0],a[i][1]));
    }

maxh.push(sum,make_pair(a[i][0],a[i][1])); gives the error

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>
mch
  • 9,424
  • 2
  • 28
  • 42
  • 2
    I do not understand - how do you want to push `int, std::pair` into container with `,vector>,int>` elements? – KamilCuk Jul 14 '20 at 07:24
  • priority_queue,int> maxh; the second argument for the template of std::priority_queue is a container. Check the definition here https://en.cppreference.com/w/cpp/container/priority_queue – Hikmat Farhat Jul 14 '20 at 07:31
  • 1
    You should try and explain what you are trying to achieve because your code is so confused it is not obvious. For instance here `maxh.push(sum,make_pair(a[i][0],a[i][1]))` you are trying to push two things onto the queue, but you can only push one thing at a time onto a priority queue. And its not possible for a queue to hold both integers and pairs at the same time. – john Jul 14 '20 at 07:37
  • 1
    If I had to guess I'd say that you are trying to put pairs onto your priority queue and order them by `sum`. If that's the case then that is not the correct way to do it. – john Jul 14 '20 at 07:42

1 Answers1

1

OK I'm taking a guess here, because you haven't explained yourself.

I'm assuming that you want to put pairs onto your priority queue and order them using these formulae that are in your code

int x1 =  a[i][0]*a[i][0];
int y1 =  a[i][1]*a[i][1]; 
    
int sum = x1 + y1;

The simplest way to do that is to use a lambda function that compares two pairs using the formula above. Here's some sample code

#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;
}

The lambda function is in the first line auto lambda = ...;. I've assumed that you want largest first. If your want smallest first just change < to > in the lambda function.

john
  • 85,011
  • 4
  • 57
  • 81
  • My first instinct was to use `std::priority_queue>>` and `push({sum, {a[i][0], a[i][1]}})`, let the implementation handle the rest. Well, certainly yours is better, takes less memory than mine! :) – brc-dd Jul 14 '20 at 08:04
  • @brc-dd That approach didn't occur to me. It may well be what the OP was trying to write. The two ways seem pretty much equivalent. – john Jul 14 '20 at 08:06