C ++我在设置我创建的类的优先级队列时遇到了问题

时间:2013-05-01 02:32:08

标签: c++ vector priority-queue

我正在尝试设置一个名为Event的类的优先级队列。在我尝试这个之前的2行中,我有2个其他优先级队列设置似乎工作正常,但我在第3个上得到一个错误。有什么建议吗?

class Event{
    enum eventType { Arrival, CPUburstCompletion, IOcompletion, TimerExpired };
    double time; // Defined as time units since the start of the simulation
};

vector<process> processTable;
int CPU;
int IO;
priority_queue< int, vector<int> > readyQueue;
priority_queue< int, vector<int> > IO_Queue;
priority_queue< Event, vector<Event> > eventQueue;

我的Event类中没有比较器功能。我把它放进去但我仍然收到错误。我没有正确写入比较器吗?

class Event{
    enum eventType { Arrival, CPUburstCompletion, IOcompletion, TimerExpired };
    double time; // Defined as time units since the start of the simulation
    bool operator()(Event& e1, Event& e2){
        if (e1.time < e2.time){
            return true;
        }
        else{
            return false;
        }
    }
};

vector<process> processTable;
int CPU;
int IO;
priority_queue< int, vector<int> > readyQueue;
priority_queue< int, vector<int> > IO_Queue;
priority_queue< Event, vector<Event> > eventQueue;

2 个答案:

答案 0 :(得分:1)

您需要在Event类中定义比较运算符,或者作为优先级队列的模板参数。

class Event{
 ...
  public: 
     bool operator<(const Event &other) const {return time < other.time;}
};

答案 1 :(得分:0)

由于您未指定Compare模板参数,因此必须在Event结构中声明operator<。队列如何知道哪个是要提取的最小元素?