error: no matching function for call to 'std::priority_queue<data>::push(int, int, int)'

What does this error mean?
occurs in line 27.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <queue>
#include <set>
#include <algorithm>
struct data {
    int sum;
    int x;
    int y;
};
using namespace std;
int main () {
    priority_queue<data> points;
    set <pair<int, int> > visit;
    vector<int> sums;
    int A [100000];
    int B [100000];
    int n;
    cin>>n;
    for (int i=0; i<n; i++) {
        cin>>A[i];
    }
    for (int i=0; i<n; i++) {
        cin>>B[i];
    }
    sort(A, A+n);
    sort(B, B+n);
    points.push(A[n-1]+B[n-1], n-1, n-1);
    visit.insert(n-1, n-1);
    for (int i=0; i<n; i++) {
        sums.push_back(points.top().sum);
        int x=points.top().x;
        int y=points.top().y;
        points.pop();
        if (visit.find({x, y-1})==visit.end()) {
            points.push(A[x]+B[y-1], x, y-1);
            visit.insert({x, y-1});
        }
        if (visit.find({x-1, y})==visit.end()) {
            points.push(A[x-1]+B[y], x-1, y);
            visit.insert({x-1, y});
        }
    }
    for (int i=0; i<n; i++) {
        cout<<sums[i]<<" ";
    }
}

Last edited on
It looks like you need to construct a data out of your params
 
points.push( data{A[n-1]+B[n-1], n-1, n-1} );

I was still getting a ton of weird errors, so I wrote
1
2
3
4
5
6
struct Compare { 
    bool operator()(data const& p1, data const& p2) 
    { 
        return p1.sum < p2.sum; 
    } 
}; 

and it worked.
Why though?
we are low on magic since the crisis of 2011
now you need to understand compiler messages and read the fucking manual

┬┐how to you expect the priority queue to sort the elements if you don't tell it how to compare them?
Doesn't the priority queue automatically sort the elements in descending order?
1
2
3
4
5
struct data {
    int sum;
    int x;
    int y;
};


What is descending order for a data? You have to tell the compiler how to sort 2 elements of data. That's what the Compare function does.
There are so many ways of writing the comparator though. One with bool operator<, one with bool operator (), with structs, with just a function. What is the difference between all of them?
`<' is quite a small name, so when you do a < b it should be extremely obvious how the comparison is being done
if that's not the case, better use a function.

there may be times when you do have operator<, but you need another behaviour, for example to compare by x or y or distance to the origin, then you may use a function
sometimes that comparison is too specific and you wouldn't need it in another place, so may use a lambda

in other cases, you need to maintain a state, for example, one algorithm to find the convex hull of a set of points sorts them by angle to one anchor point
there you may need an class with overload operator()
Last edited on
Registered users can post here. Sign in or register to post.