C++ std::priority_队列包含带有包含状态的函子的结构
关于std::priority_队列包含/,有两个很好的答案: 若我需要那个些比较结构来保存一个状态,比如流的对象,该怎么办C++ std::priority_队列包含带有包含状态的函子的结构,c++,templates,functor,C++,Templates,Functor,关于std::priority_队列包含/,有两个很好的答案: 若我需要那个些比较结构来保存一个状态,比如流的对象,该怎么办 提前感谢。您可以使用需要状态的构造函数定义比较函子: struct Compare { State state; Compare(State state) : state(state) { } bool operator()(const Item&a
提前感谢。您可以使用需要状态的构造函数定义比较函子:
struct Compare
{
State state;
Compare(State state)
: state(state)
{
}
bool operator()(const Item& a, const Item& b)
{
... // use state
}
};
并将其使用所需状态构造的实例传递给优先级队列:
priority_队列(比较(状态));
priority_queue<Item, std::vector<Item>, Compare> queue(Compare(state));