gpt4 book ai didi

c++ - 在 C++ 中使用 vector 实现简单的优先级队列

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

下面给出了下面提到的代码的错误。我哪里出错了?

error: ‘function’ is not a member of ‘std’

我想使用 C++ std lib 队列制作优先级队列,最小队列是 IceCream,它需要最少的时间来准备。我试过实现这个 -> declaring a priority_queue in c++ with a custom comparator

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <iterator>
#include <string>
#include <sstream>

using namespace std;


class IceCream
{
public:
int time_to_prep;
IceCream(int flav) {
time_to_prep = flav;
}
};

bool Compare(IceCream a, IceCream b)
{
return a.time_to_prep > b.time_to_prep;
}

int main()
{
priority_queue<IceCream, vector<IceCream>, std::function<bool(IceCream a, IceCream b)> > my_pq(Compare);
my_pq.push(IceCream(4));
my_pq.push(IceCream(33));
my_pq.push(IceCream(9));
cout << my_pq.top() << endl;
return 0;
}

最佳答案

#include <functional>

您需要这个 include 才能访问 std::function

参见:http://en.cppreference.com/w/cpp/utility/functional/function

关于c++ - 在 C++ 中使用 vector 实现简单的优先级队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39979359/

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