class Event{
public:
enum EventType { A_1, A_2, A_3, A_4};
Event(EvtType type = A_1, double etime = 0.0)
: _type(type)
, _etime(etime)
{}
EventType get_Type() const { return _type; };
double get_Time() const { return _etime; }
protected:
EventType _type;
double _etime;
};
struct EventLess{
bool operator()(const Event& lhs, const Event& rhs) const
{
return (lhs.get_Time() > rhs.get_Time());
}
};
Я могу создать мин приоритетную очередь, как показано нижеприоритет очереди из нескольких классов C++
priority_queue<Event, std::vector<Event>, EventLess> q;
Однако, если у меня есть еще один класс, как:
class Event1
{
public:
enum EventType { disruption_1, disruption_2};
Event(EvtType type = disruption_1, double htime = 0.0, double duration)
: _type(type)
, _htime(htime)
, _duration(duration)
{}
EventType get_Type() const { return _type; };
double get_Time() const { return _htime; }
double get_Duration() const { return _duration; }
protected:
EventType _type;
double _etime;
double _duration;
};
и я хочу, чтобы создать мин приоритетная очередь этих двух классов (сравнение времени). Пожалуйста, кто-нибудь продемонстрирует мне, как решить эту проблему, будет благодарен.
После получения предложения, я улучшил код, используя наследование как представлено.
#include <queue>
#include <iostream>
using namespace std;
class IEvent{
public:
IEvent(double time_e): Etime(time_e) {}
virtual double get_Time() const {return Etime;}
protected:
double Etime;
};
class Event1:public IEvent{
public:
enum EType1 { A_1, A_2, A_3, A_4};
Event1(EType1 type1, double time_e, int _speed)
: _type1(type1)
, IEvent(time_e)
, speed(_speed)
{}
virtual EType1 get_Type() const { return _type1; };
virtual double get_Time() const { return Etime; }
int get_speed() const {return speed;}
private:
EType1 _type1;
int speed;
};
class Event2:public IEvent{
public:
enum EType2 { disruption_1, disruption_2};
Event2(EType2 type2, double time_e, double duration)
: _type2(type2)
, IEvent(time_e)
, _duration(duration)
{}
virtual EType2 get_Type() const { return _type2; };
virtual double get_Time() const { return Etime; }
double get_duration() const { return _duration; }
private:
EType2 _type2;
double _duration;
};
struct IEventLess{
bool operator()(const IEvent& lhs, const IEvent& rhs) const
{
return (lhs.get_Time() > rhs.get_Time());
}
};
int main(){
priority_queue<IEvent, vector<IEvent>, IEventLess> q;
q.push(Event1(Event1::A_1, 15.0, 10));
q.push(Event2(Event2::disruption_1, 5.0, 5.0));
IEvent *evt;
evt = new q.top();
cout << evt.get_Type() << evt.get_Time() << endl;
}
Тем не менее, я до сих пор есть некоторые проблемы в вызове другой функции в производном классе, и немного сбивает с толку при вызове функции верхней(), как показано в главной функции.
Пожалуйста, помогите мне решить проблему.
спасибо.
Одним из способов может быть унаследована от общей базы и 'оператора <'. – user4581301