我有一个名为 getout 的类(没有构造函数)。在那个类中,我有一些私有变量,它们是优先级队列。使用我应该创建的自定义比较器函数初始化优先级队列:
priority_queue<tile, vector<tile>, ****insert comparator here***> primary;
我知道可以使用类或结构来编写自定义比较器。但是我不能这样做(我确定有办法)。为什么在这个比较器中我使用与我的班级有关的函数。我决定把我的比较器写成一个普通的 bool 函数,如下所示:
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
};
我正在使用的与我的班级有关的函数是 get_row()、get_col()。我不想通过使它们成为我的 tile 结构的成员变量来解决这个问题。
如何定义布尔函数形式的优先级队列比较器?
一切都在我的课堂上。
我努力了:
priority_queue<tile, vector<tile>, function<bool(tile, tile)>> primary(less_than);
但我收到错误“未知类型名称小于”。我是否正确实现了上述代码?还有其他方法可以做到这一点吗?
(包括所有必要的库)
谢谢!!