Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多类C++优先队列_C++_Priority Queue - Fatal编程技术网

多类C++优先队列

多类C++优先队列,c++,priority-queue,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; };

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

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());
        }
};
我想创建这两个类时间比较的最小优先级队列。 请任何人向我演示如何解决这个问题,我将不胜感激

在收到建议后,我使用前面介绍的继承改进了代码

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

尽管如此,我在调用派生类中的其他函数时仍然存在一些问题,并且在调用主函数中显示的top函数时有点混乱

请任何人帮我解决这个问题

谢谢。

让Event和Event1都从一个公共基础继承

    #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;
然后,您可以将基本指针存储在队列中,并在需要时转换到具体的子对象

std::优先级队列q

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

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

一种方法是从公共基和运算符继承。如果要修改类以执行继承,至少要将get_Time延迟到公共虚拟impl并删除动态强制转换!您能为此添加适当的比较吗?@RichardHodges已更新,但使用不同的EventType枚举,他们仍然需要动态\u强制转换。虽然这两个枚举也可能在基类中组合成一个,但由于这两个类的接口不同,我认为这个问题不适合继承。一种不是另一种,也不是普通类型。我认为变量/静态访问者方法更合适,因为它允许通过外部自由函数定义两个或多个类型之间的关系-至少考虑到OP问题中可用的有限信息,这是我的观点。现实情况可能会有所不同……你很有可能让etime成为基类中受保护的成员,并摆脱虚拟方法。我已经尝试过使用boost。我有几个问题。我需要包括吗?当我尝试构建时,我得到了错误信息,显示对std::priority的调用没有匹配的函数queue@Rocky根据你的Boost和/或C++版本,你可能没有LAMBDA支持。您可能需要构建一个派生自boost::static\u visitor的小函子。文档中有一些例子,非常感谢。现在它起作用了。但是,在这种情况下,您可以演示如何将前面的元素置于队列之外吗?Cheers@Youmean take?@Rocky更新为包括从优先级队列顶部弹出事件,并使用静态访问者对其进行操作。优先级队列只有“推”、“弹出”和“顶部”。除非从优先级队列派生并对c成员进行一些欺骗,否则不能按顺序删除事件。
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() {...}
}
auto Time = q.front()->get_Time();
#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);
}