多个类C ++的优先级队列

时间:2016-09-12 14:22:51

标签: c++ priority-queue

~

我可以创建最小优先级队列,如下所示

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

但是,如果我有另一个类,如:

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

}

尽管如此,在派生类中调用其他函数时仍然存在一些问题,并且在main函数中调用top()函数时会有点混乱。

请有人帮我解决。

谢谢。

2 个答案:

答案 0 :(得分:6)

boost::variant不需要公共基类或继承:

#include <queue>
#include <boost/variant.hpp>

class Event{
public:
    enum EventType { A_1, A_2, A_3, A_4};

    Event(EventType 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;
};

inline double get_time(const Event& e) {
    return e.get_Time();
}

class Event1
{
public:
    enum EventType { disruption_1, disruption_2};

    Event1(EventType type = disruption_1, double htime = 0.0, double duration = 0)
    : _type(type)
    , _etime(htime)
    , _duration(duration)
    {}

    EventType get_Type() const { return _type; };
    double get_Time() const { return _etime; }
    double get_Duration() const { return _duration; }

protected:
    EventType _type;
    double _etime;
    double _duration;
};

inline double get_time(const Event1& e) {
    return e.get_Time();
}


// name predicates properly
struct EventGreater{

    template<class L, class R>
    bool operator()(const L& lhs, const R& rhs) const
    {
        return get_time(lhs) > get_time(rhs);
    }
};

template<class...Ts>
double get_time(const boost::variant<Ts...>& var)
{
    return boost::apply_visitor([](auto& x) { return get_time(x); }, var);
}

struct event_handler : boost::static_visitor<void>
{
    void operator()(const Event& e) const
    {
        // handle an Event in here
    }

    void operator()(const Event1& e) const
    {
        // handle an Event1 in here
    }

};

int main()
{
    using EventVariant = boost::variant<Event, Event1>;
    // we all know that a priority queue inverts the predicate
    std::priority_queue<EventVariant, std::vector<EventVariant>, EventGreater> q;


    q.push(Event());
    q.push(Event1());

    //
    // remove an item and action it
    auto ev = std::move(q.top());
    q.pop();

    boost::apply_visitor(event_handler(), ev);
}

答案 1 :(得分:2)

EventEvent1都从公共基础继承。

class IEvent
{
public:
  virtual double get_Time() = 0;
};

class Event : public IEvent
{
public:
  virtual double get_Time() {...}
}

class Event1 : public IEvent
{
public:
  virtual double get_Time() {...}
}

然后,您可以将基本指针存储在队列中,并在需要时强制转换为具体子项。

std::priority_queue<std::shared_ptr<IEvent>, std::vector<IEvent>, IEventLess> q;

auto Time = q.front()->get_Time();