Search code examples
priority-queuec++98

Issues with custom compare for std::priority_queue in c++98


I'm trying to create varaible of type 'std::priority_queue' like this:

struct
{
    bool operator()(std::vector<int> v1, std::vector<int> v2){return (v1[2] + v1[3]) > (v2[2] + v2[3]);};
} queueCompare;

std::priority_queue< std::vector<int>, std::vector< std::vector<int> >, queueCompare > queue;

But the compiler gives me this error:

expected a type, got `queueCompare'

I don't exactly understand what is wrong here. As far as I know, this should be a correct way of creating it.


Solution

  • that is invalid struct definition. correctly struct definition is not struct {...} name but struct name {...}
    In addition, I recommend using const reference in the compare method to avoid unnecessary copies

    struct queueCompare
    {
        bool operator()(const std::vector<int>& v1, const std::vector<int>& v2){
            return (v1[2] + v1[3]) > (v2[2] + v2[3]);
        };
    };