在C ++中使用向量实现简单优先级队列

时间:2016-10-11 14:17:04

标签: c++ class queue compare priority-queue

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

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

1 个答案:

答案 0 :(得分:6)

#include <functional>

您需要此包含才能访问std::function

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