ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 2.7 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://docs.oracle.com/cd/E19205-01/819-3703/11_2.htm |
| Last Crawled | 2026-01-15 20:55:49 (2 months ago) |
| First Indexed | 2019-07-16 07:58:38 (6 years ago) |
| HTTP Status Code | 200 |
| Meta Title | 11.2 The priority queue Operations |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | A
priority queue
is a data structure that can hold elements of type
T
and implement the five operations given in
Table 17
:
Table 17 -- Priority queue operations
Function
Implemented operation
push(T)
Adds a new value to the collection being maintained
top()
Returns a reference to the smallest element in the collection
pop()
Deletes the smallest element from the collection
size()
Returns the number of elements in the collection
empty()
Returns 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
<
, 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 chapter. As with all the containers in the Standard Library, there are several 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.
The
priority queue
datatype 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 C++ Library that can be used to construct
priority queues
:
vector
s
or
deque
s
. By default, a
priority_queue
will use
vector
.
11.2.1 Declaration and Initialization of priority queue
The following illustrates the declaration of several
priority queue
s
3
:
priority_queue< int > queue_one; //uses vector and less<int>
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> >
queue_five(aVector.begin(), aVector.end(), eventComparison);
The
queue
s constructed out of
vector
s tend to be somewhat smaller, while the
queue
s constructed out of
deque
s can be somewhat faster, particularly if the number of elements in the
queue
varies widely over the course of execution. However, these differences are slight, and either form generally works in most circumstances.
Because the
priority queue
data structure does not itself know how to construct iterators, very few of the algorithms noted in
Chapter 13
can be used with
priority queue
s. Instead of iterating over values, a typical algorithm that uses a
priority queue
constructs a loop, which repeatedly pulls values from the structure (using the
top()
and
pop()
operations) until the collection becomes empty (tested using the
empty()
operation). The example program described in
Section 11.3
will illustrate this use.
A
priority queue
is implemented by internally building a data structure called a
heap
. Abstractly, a
heap
4
is a binary tree in which the value associated with every node is smaller than or equal to the value associated with either child node.
©Copyright 1998, Rogue Wave Software, Inc.
Send
mail
to report errors or comment on the documentation.
OEM Release, June 1998 |
| Markdown | [](https://docs.oracle.com/cd/E19205-01/index.htm)
[](https://docs.oracle.com/cd/E19205-01/819-3703/11_1.htm)[](https://docs.oracle.com/cd/E19205-01/819-3703/index.htm)[](https://docs.oracle.com/cd/E19205-01/819-3703/booktoc.htm)[](https://docs.oracle.com/cd/E19205-01/819-3703/tindex.htm)[](https://docs.oracle.com/cd/E19205-01/819-3703/11_3.htm)
## 11\.2 The priority queue Operations
A ***priority queue*** is a data structure that can hold elements of type T and implement the five operations given in [Table 17](https://docs.oracle.com/cd/E19205-01/819-3703/11_2.htm#Table%2017):
| **Function** | **Implemented operation** |
|---|---|
| push(T) | Adds a new value to the collection being maintained |
| top() | Returns a reference to the smallest element in the collection |
| pop() | Deletes the smallest element from the collection |
| size() | Returns the number of elements in the collection |
| empty() | Returns 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 \< , 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 chapter. As with all the containers in the Standard Library, there are several constructors. The *default constructor* requires either no arguments or the optional comparison function. An *alternative constructor* takes an iterator [***pair***](https://docs.oracle.com/cd/stdref/pai_5818.htm), 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.
The ***priority queue*** datatype 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 C++ Library that can be used to construct ***priority queues***: ***[vector](https://docs.oracle.com/cd/stdref/vec_0251.htm)****s* or [***deque***](https://docs.oracle.com/cd/stdref/deq_4164.htm)*s*. By default, a [***priority\_queue***](https://docs.oracle.com/cd/stdref/pri_2327.htm) will use ***vector***.
[11\.2.1 Declaration and Initialization of priority queue]()
The following illustrates the declaration of several ***priority queue***s[3](https://docs.oracle.com/cd/E19205-01/819-3703/endnotes.htm#fn3):
The [***queue***](https://docs.oracle.com/cd/stdref/que_0953.htm)s constructed out of ***[vector](https://docs.oracle.com/cd/stdref/vec_0251.htm)***s tend to be somewhat smaller, while the ***queue***s constructed out of [***deque***](https://docs.oracle.com/cd/stdref/deq_4164.htm)s can be somewhat faster, particularly if the number of elements in the ***queue*** varies widely over the course of execution. However, these differences are slight, and either form generally works in most circumstances.
Because the ***priority queue*** data structure does not itself know how to construct iterators, very few of the algorithms noted in [Chapter 13](https://docs.oracle.com/cd/E19205-01/819-3703/13.htm) can be used with ***priority queue***s. Instead of iterating over values, a typical algorithm that uses a ***priority queue*** constructs a loop, which repeatedly pulls values from the structure (using the top() and pop() operations) until the collection becomes empty (tested using the empty() operation). The example program described in [Section 11.3](https://docs.oracle.com/cd/E19205-01/819-3703/11_3.htm) will illustrate this use.
A ***priority queue*** is implemented by internally building a data structure called a ***heap***. Abstractly, a ***heap***[4](https://docs.oracle.com/cd/E19205-01/819-3703/endnotes.htm#fn4) is a binary tree in which the value associated with every node is smaller than or equal to the value associated with either child node.
***
[](https://docs.oracle.com/cd/E19205-01/819-3703/11_1.htm)[](https://docs.oracle.com/cd/E19205-01/819-3703/index.htm)[](https://docs.oracle.com/cd/E19205-01/819-3703/booktoc.htm)[](https://docs.oracle.com/cd/E19205-01/819-3703/tindex.htm)[](https://docs.oracle.com/cd/E19205-01/819-3703/11_3.htm)
©Copyright 1998, Rogue Wave Software, Inc.
Send [mail](mailto:onlinedocs@roguewave.com) to report errors or comment on the documentation.
OEM Release, June 1998 |
| Readable Markdown | null |
| Shard | 75 (laksa) |
| Root Hash | 4495097500273443275 |
| Unparsed URL | com,oracle!docs,/cd/E19205-01/819-3703/11_2.htm s443 |