The priority queue operations, Declaration and initialization of priority queue – HP Integrity NonStop H-Series User Manual

Page 122

Advertising
background image

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

The Priority Queue Operations

A priority queue is a data structure that can hold elements of type T and that implements the
following five operations:

push(T) add a new value to the collection being maintained

top()

return a reference to the smallest element in collection

pop()

delete the smallest element from the collection

size()

return the number of elements in the collection

empty() return true if the collection is empty

Elements of type T must be comparable to each other, either through the use of the default less
than operator (the < operator), or through a comparison function passed either as a template
argument or as an optional argument on the constructor. The latter form will be illustrated in the
example program provided later in this section. As with all the containers in the Standard Library,
there are two constructors. The default constructor requires either no arguments or the optional
comparison function. An alternative constructor takes an iterator pair, and initializes the values in
the container from the argument sequence. Once more, an optional third argument can be used to
define the comparison function.

Initializing Queues from other containers

The priority queue data type is built on top of a container class, which is the structure actually
used to maintain the values in the collection. There are two containers in the standard library that
can be used to construct priority queues: vectors or deques.

Declaration and Initialization of priority queue

The following illustrates the declaration of several priority queues:

priority_queue< int, vector<int> > queue_one;
priority_queue< int, vector<int>, greater<int> > queue_two;
priority_queue< double, deque<double> >
queue_three(aList.begin(), aList.end());
priority_queue< eventStruct, vector<eventStruct> >
queue_four(eventComparison);
priority_queue< eventStruct, deque<eventStruct> >

Advertising
This manual is related to the following products: