Blog

Priority Queue

May 30, 2006 — Whitney Young

This is more of a note to myself, but I've had to look up how to initialize a priority queue with a custom comparison function in C++ too many times (and it's always hard to find). It's really not hard, there's just not one good online source for the STL (someone please point one out if there is - or even a good pocket reference type book). Anyway, here's the code.... not too hard:

#include "event.h"
#include <queue>
using std::priority_queue;
class greater {
  public: int operator() (Event *x, Event *y) { return *x > *y; }
};
priority_queue< Event *, vector<Event *>, greater > _event_queue;

4 Comments Tags: C++, Code, Typo