I created a priority_queue and define a custom comparator(a struct) which takes an 2D vector in the constructor. It compiles and runs successfully to the point of declaring that priority queue. However, if I try to add an element into the priority queue, it pops me error:
error: request for member 'push' in 'minHeap', which is of non-class type 'std::priority_queue, std::vector >, MyComparator>(MyComparator)' minHeap.push(make_pair(0, 1));
I would like to know what is the problem?
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct MyComparator{
vector<vector<int>> array2D;
MyComparator(const vector<vector<int>>& arrays){
array2D = arrays;
}
bool operator()(const pair<int, int> &p1, const pair<int, int> &p2){
return array2D[p1.second][p1.first] > array2D[p2.second][p2.first];
}
};
int main() {
vector<vector<int>> arrays = {{1, 3, 5, 7}, {2, 4, 6},{0, 8, 9, 10, 11}};
priority_queue<pair<int, int>, vector<pair<int, int>>, MyComparator> minHeap(MyComparator(arrays));
//work well without error above
minHeap.push(make_pair(0, 1)); //pop me error
return 0;
}