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;

Tags: C++, Code, Typo

Comments
philip Oct 19, 2006

good post - changed from Event to my class type and it worked perfectly :) Thanks for putting it up

[email protected] Nov 15, 2006

hello sir, i m looking for the source code for t implementation of the Priority Queue in c language.. so plz if u hv sme thnG related to it kindally send me!! Thnxs!

Evan May 30, 2007

Hello, one year ago today it seems you posted this. And today I am taking advantage of this, its works quite brilliantly, thanks so much!

tony k poulose Oct 1, 2007

hello sir I would like to get a source code of c language which implimented as priority gueue