A priority_queue is a data structure useful in problems where you need to rapidly and repeatedly find and remove the largest element from a collection of values. An everyday example of a priority queue is the to do list of tasks waiting to be performed that most of us maintain to keep ourselves organized. Some jobs, such as clean desktop, are not imperative and can be postponed arbitrarily. Other tasks, such as finish report by Monday or buy flowers for anniversary, are time-crucial and must be addressed more rapidly. Thus, we sort the tasks waiting to be accomplished in order of their importance, or perhaps based on a combination of their critical importance, their long term benefit, and the fun we will have doing them, and choose the most pressing.
A more computer-related example of a priority queue is the list of pending processes maintained by an operating system, where the value associated with each element is the priority of the job. For example, it may be necessary to respond rapidly to a key pressed at a terminal before the data is lost when the next key is pressed. On the other hand, the process of copying a listing to a queue of output waiting to be handled by a printer is something that can be postponed for a short period, as long as it is handled eventually. By maintaining processes in a priority queue, those jobs with urgent priority are executed prior to any jobs with less urgent requirements.
Simulation programs use a priority queue of future events. The simulation maintains a virtual clock, and each event has an associated time when the event will take place. In such a collection, the element with the smallest time value is the next event that should be simulated. These are only a few instances of the types of problems for which a priority_queue is a useful tool. You probably have encountered others, or you soon will.
Some developers may feel the term priority queue is a misnomer. The data structure is not a queue in the sense that we used the term in Chapter 10, since it does not return elements in a strict first-in, first-out sequence. Nevertheless, the name is now firmly associated with this particular datatype.
Programs that use the priority queue data abstraction should include the queue header file: