boost :: heap :: priority_queue vs std :: priority_queue的比较器

Cec*_*lia 2 c++ boost std

我正在尝试为自定义Edge类设置优先级队列,其中Edges将按权重进行比较.

class Edge 
{
public:
    int index;
    double weight;
std::pair<int, int> vertices;

Edge(int i, double w, int start, int end)
{
    index = i;
    weight = w;
    vertices.first = start;
    vertices.second = end;
}
}; 
Run Code Online (Sandbox Code Playgroud)

我成功地在自定义类上实现了一个std::priority_queue使用STL优先级队列,并使用http://gigi.nullneuron.net/comp/cpp-stl-priority-queue.php作为参考,使用这样的比较器,

struct EdgeCompare
{
    bool operator()(const Edge &e1, const Edge &e2) const
    {
        return e1.weight < e2.weight;
    }
}

std::priority_queue<Edge, std::vector<Edge>, EdgeCompare> queue;
Run Code Online (Sandbox Code Playgroud)

但是,我意识到这std::priority_queue不提供迭代器.这是我真正想要的功能,所以我决定切换到boost::heap::priority_queue.我知道boost::heap::priority_queue有一个构造函数将设置自定义比较器.但是,我找不到任何解释如何最好地传递函数的示例.我显然不能使用我用过的相同语法std::priority_queue.

我试过了

EdgeCompare comparator;
boost::heap::priority_queue<Edge> queue2(comparator.operator);
Run Code Online (Sandbox Code Playgroud)

但我得到一个错误,告诉我指定一个运算符.

我也试过重命名该函数,但后来我得到一个错误,告诉我一个函数的指针只能用于调用该函数.

声明和传递比较器的正确方法是什么?

eca*_*mur 6

是的,选项界面有些记录不足.您可以指定以下选项:

boost::heap::priority_queue<Edge, boost::heap::compare<EdgeCompare>>
Run Code Online (Sandbox Code Playgroud)