0

I have 3 classes :

  • Event wich is purely abstract
  • Arrive wich derivate from event
  • Leave wich also derivate from event

Event is defined as follow :

class Event{
protected:
    double time;
}

I need to store events into a queue and retrieve them in ascending order based on event time.

To do that I created a priority_queue like this :

priority_queue<Event*, vector<Event*>, greater<>>

Comparators have been defined for Event based on time.

I push into this queue objects of type Arrive and Leave.

Everything works great except that the queue return object not based on the smallest time but on the smallest pointer address value.

  • Please read [What is a smart pointer and when should I use one?](https://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one) – François Andrieux Sep 13 '19 at 13:14
  • 1
    @CuriousLearner The queue compares pointers Event *. You have to write you own comparator using for example a lambda expression. Instead it seems you defined the comparison for objects of the type Event but the comparator is not used by the queue. – Vlad from Moscow Sep 13 '19 at 13:15
  • @CuriousLearner The linked duplicate contains lots of example `Compare` implementations. All you have to do is implement `operator()(const Node*, const Node*)` instead of `operator()(Node, Node)`. – Max Langhof Sep 13 '19 at 13:20
  • Thanks to all your answers ! I solved the problem using a multiset instead and by defining a class with `operator()(const Event*, const Event*)`. – CuriousLearner Sep 13 '19 at 13:57

0 Answers0