Priority Queue in C++ STL (original) (raw)

A Priority Queue adds and removes elements according to priority.

#include #include using namespace std;

int main(){

// Create a max-heap priority queue (default)
priority_queue<int> pq;

pq.push(30);
pq.push(10);
pq.push(20);
pq.push(40);

cout << "Elements removed from priority queue in order:\n";

while (!pq.empty()){
    cout << pq.top() << " ";
    pq.pop();
}

return 0;

}

`

Output

Elements removed from priority queue in order: 40 30 20 10

Syntax

Priority queue is defined as std::priority_queue inside header file.

priority_queue<T, c, comp> pq;

where,

Declaration and Initialization

Declaration of a priority queue means creating it using the STL choosing whether it's a max-heap or min-heap.

priority_queuepq;

priority_queue<int,vector,greater>pq;

Initialization means adding elements to it so it can manage them based on their priority.

pq.push(10);
pq.push(5);
pq.push(20);

Basic Operations

Here are the basic operations that can be performed on a priority queue:

1. Inserting Elements

Elements can be inserted in the priority queue using push() method. After insertion, priority queue reorganize itself in such a way that the highest priority element is always at the top.

C++ `

#include #include using namespace std;

int main(){ priority_queue pq;

// Inserting elements
pq.push(9);
pq.push(8);
pq.push(6);

return 0;

}

`

2. Accessing Elements

Only the top element of the priority queue can be accessed using top() method. It is the element with the highest priority in priority queue.

C++ `

#include #include using namespace std;

int main(){ priority_queue pq; pq.push(9); pq.push(8); pq.push(6);

// Accessing top element
cout << pq.top();
return 0;

}

`

3. Deleting Elements

In priority queue, deletion can only be done from the top of the priority queue using **pop()method. It means that we can only remove the element with highest priority in one move. After deletion, the priority queue rearranges itself such that the next greatest priority element becomes the top element.

C++ `

#include #include using namespace std;

int main(){ priority_queue pq; pq.push(9); pq.push(8); pq.push(6);

// Delete top element
pq.pop();
cout << pq.top();

return 0;

}

`

**Explanation: In this example, we deleted the top element using pop() method, which is 9. This shifts the next greatest element 8 to the top.

4. empty()

This checks whether the priority queue is empty. It returns true if the priority queue has no elements; otherwise, it returns false.

C++ `

#include #include using namespace std;

int main(){

priority_queue<int> pq;
if (pq.empty()){
    cout << "Priority queue is empty. " << endl;
}
pq.push(50);
pq.push(30);
if (!pq.empty()){
    cout << "Priority queue is not empty. Top element " << pq.top() << endl;
}
return 0;

}

`

Output

Priority queue is empty. Priority queue is not empty. Top element 50

5. Size of priority queue

The size() function in a priority queue returns the number of elements currently in the priority queue.

C++ `

#include #include using namespace std;

int main(){

priority_queue<int> pq;

// Add elements to the priority queue
pq.push(100);
pq.push(50);

// Dispaly the size of priority queue
cout << "Size of priority queue: " << pq.size() << endl;

// Remove one element
pq.pop();

// Display size again
cout << "Size after one pop: " << pq.size() << endl;
return 0;

}

`

Output

Size of priority queue: 2 Size after one pop: 1

Pseudo Traversal

In priority queue, we can only access top element of the priority queue, so we cannot directly traverse it. However, we can create a copy of the priority queue, access and delete the top element and repeat this process until the copied priority queue is empty. In this way, we can effectively traverse all the elements of the priority queue.

C++ `

#include #include using namespace std;

int main(){ priority_queue pq; pq.push(9); pq.push(8); pq.push(6);

// Create a copy
priority_queue<int> temp(pq);
while (!temp.empty())
{
    cout << temp.top() << " ";
    temp.pop();
}
return 0;

}

`

Changing Priority Queue Order

All the above operations are demonstrated on a priority queue implementing max heap. This can be changed by using a custom comparator in which you define the priority parameter and how to compare them.

If you only want to assign highest priority to the smallest element (min-heap), then inbuilt greater functional object can be used.

C++ `

#include #include using namespace std;

int main(){

priority_queue<int, vector<int>, greater<int>> pq;
pq.push(9);
pq.push(8);
pq.push(6);

auto temp = pq;
while (!temp.empty()){
    cout << temp.top() << " ";
    temp.pop();
}
return 0;

}

`

**Explanation: In the above program, even though vector is default container, we still have to pass it because the comparator is third template argument. So, we have to pass all the arguments before it.

Priority Queue vs Queue

Feature Queue Priority Queue
Order of Removal FIFO (First-In, First-Out) Based on priority
Element Priority All elements treated equally. Each element has a priority.
Use case Task scheduling, print queue CPU scheduling, Dijkstra's Algorithm
Default Behavior Inserts at rear, removes front Inserts anywhere, removes highest priority
STL Class in C++ queue priority_queue

All Member Functions

Following is the list of all member functions of std::priority_queue class in C++:

**Function **Definition
empty() Returns whether the priority queue is empty.
size() Returns the size of the priority queue.
top() Returns top element of the priority queue
push() Adds the element into the priority queue.
pop() Deletes the top element of the priority queue.
swap() Used to swap the contents of two priority queues provided the queues must be of the same type, although sizes may differ.
emplace() Used to insert a new element into the priority queue container.