Search code examples
c++algorithmc++11vectorpriority-queue

C++ Priority Queue with Custom Compare Function not Behaving Correctly on Push()


I have defined a C++ priority queue like this:

priority_queue<pair<int,int>, vector<pair<int,int>>, decltype(&mygreater)> frontier(&mygreater);

with a custom mygreater function() like this:

bool mygreater(pair<int,int> v1, pair<int,int> v2) {
        return v1.first > v2.first;
}

However, when I try to push a vector of pairs of ints onto the priority queue, I keep getting a ton of errors, including:

error: no matching function for call to ‘std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int> >, bool (*)(std::pair<int, int>, std::pair<int, int>)>::push(std::vector<std::pair<int, int> >&)’ frontier.push(temp);

Might anyone know what is causing the errors on my attempts to push to the priority queue? Any help is greatly appreciated.


Solution

  • This error has nothing to do with your custom comparison. You get the same error if you do this:

    std::priority_queue<int> q;
    std::vector<int> v;
    q.push(v);
    

    std::priority_queue::push takes a single object of whatever type you're storing, not a whole vector of them. The type you're storing is pair<int,int>, but you're trying to push vector<pair<int,int>>.