Like most of the standard library, priority_queue is designed to be very flexible so it can meet the needs of many problems.
The second argument exists because you might want to use a different container for the underlying storage. The reference page that kbw mentions indicates that std::vector and std::deque will both work.
The way I understand it is the a priority queue orders the list from greatest to smallest |
Technically it does not order the list, at least not completely. It just ensures that when you remove the top item, it will be the largest (by default). If that's all you need to do then priority_queue is faster than a fully sorted container.
The third argument lets you define how the items compare to each other. In other words, what you mean by "largest". Suppose your problem met all the requirements for using a prioirty queue, but you wanted to remove the smallest item, not the largest. Wouldn't it be terrible if you couldn't use
std::priority_queue
? It meets all your needs except one silly tiny one. For this reason, priority_queue (and all the containers that express an ordering) let you specify the function that orders them.
A great example is an event driven simulator. Events are processed in the order that they occur. So you want to shove events into a container and then each time you remove one, you want the one that occurs earliest - the one whose time stamp is
less than all the others, not greater.
Hope this helps.