Custom comparator for a priority queue without defining a nested class

133 Views Asked by At

I have a class called getout (no constructor). Within that class I have some private variables that are priority queues. The priority queues are initialized with a custom comparator function that I am supposed to create:

priority_queue<tile, vector<tile>, ****insert comparator here***> primary;

I understand that custom comparators can be written using a class or struct. However I cannot do it this way (Im sure there's a way). The reason why is within this comparator I use functions pertaining to my class getout. I decided to write my comparator as a regular bool function as follows:

class escape{

public:
//grabs row of the tile
int get_row(int index){
    return floor(index/size);
}

//grabs column of the tile
int get_col(int index){
    return index - get_row(index)*size;
}

//stores information about each tile of the grid
struct tile{
    int index;
};

//returns the index provided a row and column
int get_index(int row, int col){
    return row*size + col;
}
//comparator less_than
bool less_than(const tile &t1, const tile &t2)
{
    if(t1.rubble_amount == t2.rubble_amount){
        //return object with lower column value
        if(get_col(t1.index) == get_col(t2.index)){
            return get_row(t1.index) > get_row(t2.index);
        }
        //if column values are same, return object with lower row
        else if(get_col(t1.index) > get_col(t2.index)){
            return true;
        }
    }//if
    return t1.rubble_amount > t2.rubble_amount;
}//comparator less_than

};

The functions pertaining to my class I am using are get_row(), get_col(). I do not want to resolve this by making them member variables of my tile structs.

How do I define the comparator of my priority queue that is of the form of a bool function?

Everything is in my class getout.

I have tried:

priority_queue<tile, vector<tile>, function<bool(tile, tile)>> primary(less_than);

But I am getting an error "Unknown type name less_than". Am I implementing the above code correctly ? Is there another way I can do this?

(all necessary libraries are included)

Thanks!!

0

There are 0 best solutions below