gpt4 book ai didi

c++ - boost::heap::priority_queue 与 std::priority_queue 的比较器

转载 作者:搜寻专家 更新时间:2023-10-31 00:14:09 26 4
gpt4 key购买 nike

<分区>

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

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;
}
};

我使用 STL Priority Queue on custom class 成功实现了 std::priority_queuehttp://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;

然而,我随后意识到 std::priority_queue 不提供迭代器。这是我非常想要的功能,因此我决定切换到 boost::heap::priority_queue。我知道 boost::heap::priority_queue 有一个可以设置自定义比较器的构造函数。但是,我找不到任何示例来解释如何最好地传递该函数。我显然不能使用与 std::priority_queue 相同的语法。

我试过了

EdgeCompare comparator;
boost::heap::priority_queue<Edge> queue2(comparator.operator);

但我收到一个错误,告诉我指定一个运算符。

我也尝试重命名该函数,但随后出现错误,告诉我指向函数的指针只能用于调用该函数。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com