gpt4 book ai didi

c++ - 带有自定义比较器的 priority_queue 返回错误的堆顶

转载 作者:行者123 更新时间:2023-11-28 06:12:15 25 4
gpt4 key购买 nike

我想知道为什么下面的代码会返回以 10 作为顶部元素的元素

class mycomparison
{
public:
bool operator() (pair<int, Node*> e1, pair<int, Node*> e2) const {
return e1.first < e2.first;
};
};

priority_queue<pair<int, Node*>, vector<pair<int, Node*>>, mycomparison> queue;
queue.push(make_pair(4, &root));
queue.push(make_pair(10, &root));
auto var = queue.top();

它不应该返回第 4 个元素吗?

最佳答案

C++ 优先级队列是一个最大堆; 最大的 元素在顶部

关于c++ - 带有自定义比较器的 priority_queue 返回错误的堆顶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31035166/

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