class PriorityQueue<ValueType>enqueue (add value with assigned priority)
and dequeue (remove value with most urgent priority). Both of these operations run in O(logN) time.
Priority queues do not allow access to elements by index, nor can you iterate through the elements using the range-based for loop.
| Constructor | ||
| O(1) | Initializes a new priority queue, which is initially empty. | |
| Methods | ||
| O(N) | Reassigns the priority of an existing value in the priority queue. | |
| O(1) | Removes all elements from the priority queue. | |
| O(log N) | Removes and returns the frontmost (most urgent) value. | |
| O(log N) | Adds value to the priority queue with the specified priority. |
|
| O(N log N) | Returns true if the two priority queues contain the same elements with the same priorities. |
|
| O(1) | Returns true if the priority queue contains no elements. |
|
| O(1) | Returns the value of the frontmost (most urgent) element, without removing it. | |
| O(1) | Returns the priority of the frontmost (most urgent) element, without removing it. | |
| O(1) | Returns the number of values in the priority queue. | |
| O(N) | Returns a printable string representation of the priority queue. | |
| Operators | ||
| O(N log N) | Returns true if pq1 and pq2 contain the same elements. |
|
| O(N log N) | Returns true if pq1 and pq2 have different contents. |
|
| O(N) | Outputs the contents of the priority queue to the given output stream. | |
| O(NlogN) | Reads the contents of the given input stream into the priority queue. | |
PriorityQueue();
Usage:
PriorityQueue<ValueType> pq;
void changePriority(ValueType value, double newPriority);
value to the specified newPriority,
which must be at least as urgent as (≤) the value's previous
priority.
This function raises an error if the value is not present in this priority queue or if the new priority passed is not at least as urgent as its current priority.
Usage:
pq.changePriority(value, newPriority);
void clear();
Usage:
pq.clear();
ValueType dequeue();
This function throws an error if the queue is empty.
Usage:
ValueType front = pq.dequeue();
void enqueue(const ValueType& value, double priority);
value to the queue with the specified priority.
Lower priority numbers correspond to more urgent priorities, which
means that all priority 1 elements are dequeued before any
priority 2 elements.
Negative priorities are allowed and are not treated differently from other values. That is, a priority of -1 comes before one of 0, which comes before 1, 2, 3, etc.
This function throws an error if NaN (not-a-number) is passed as the priority.
Usage:
pq.enqueue(value, priority);
bool equals(const PriorityQueue& pq) const;
true if the two queues contain exactly the same element values with the same priorities.
Identical in behavior to the == operator.
Usage:
if (pq.equals(pq2)) ...
bool isEmpty() const;
true if the priority queue contains no elements.
Usage:
if (pq.isEmpty()) ...
const ValueType& peek() const;
This function throws an error if the queue is empty.
Usage:
ValueType front = pq.peek();
double peekPriority() const;
This function throws an error if the queue is empty.
Usage:
double priority = pq.peekPriority();
int size() const;
Usage:
int n = pq.size();
string toString() const;
"{pri1:value1, pri2:value2, pri3:value3}".
The order of elements in the list is unpredictable.
Usage:
string str = pq.toString();
ostream& operator<<(const PriorityQueue& pq);Outputs the contents of
pq to the given output stream.
The format is the same as specified by toString.
Usage:
cout << queue << endl;
istream& operator>>(PriorityQueue& pq);Reads the contents of the given input stream into
pq. Any previous
contents of the priority queue are replaced.
The input is expected to be in the form {pri1:value1, pri2:value2}
where the elements can be listed in any order. If unable to read a proper
priority queue from the stream, the operation results in a stream fail state.
Usage:
if (infile >> queue) ...