Mohit Kumar
Mohit Kumar

Reputation: 510

Implementing simple priority queue using vector in C++

The following gives an error for the code mentioned below. Where I have gone wrong ?

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

I want to make priority queue using C++ std lib queue, and min the queue is that IceCream which takes least time to prep. I have tried implementing this -> 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;
}

Upvotes: 2

Views: 2541

Answers (1)

sji
sji

Reputation: 1907

#include <functional>

You need this include to get access to std::function

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

Upvotes: 6

Related Questions