English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

c++ Priority Queue (priority_queue)

C ++The priority queue in it is a derived container in STL, which only considers the highest priority element. The queue follows the FIFO strategy, while the priority queue pops elements based on priority, that is, the element with the highest priority is popped first.

It is similar to a normal queue in some aspects, but it is different in the following aspects:

  • In the priority queue, each element in the queue is associated with a certain priority, but the priority does not exist in the queue data structure.

  • The element with the highest priority in the priority queue will be deleted first, and the queue followsFIFO (First In, First Out)Strategy, this meansFirstThe elements inserted will be deleted first.

  • If there are multiple elements with the same priority, the order of the elements in the queue will be considered.

Note: The priority queue is an extended version of a normal queue, but the element with the highest priority will be deleted first from the priority queue.

Syntax of the priority queue

priority_queue<int> variable_name;

Let's understand the priority queue through a simple example.

In the above figure, we inserted elements using the push() function, and the insertion operation is the same as that of a normal queue. However, when we use the pop() function to delete elements from the queue, the element with the highest priority will be deleted first.

Member functions of the priority queue

FunctionDescription
push()It inserts a new element into the priority queue.
pop()It removes the highest priority element from the queue.
top()This function is used to address the top element of the priority queue.
size()Returns the size of the priority queue.
empty()It verifies whether the queue is empty. Based on the verification, it returns the status of the queue.
swap()It exchanges the elements of the priority queue with another queue of the same type and size.
emplace()It inserts a new element at the top of the priority queue.

Let's create a simple priority queue program.

#include <iostream>
#include<queue>
using namespace std;
int main()
{
 priority_queue<int> p;  // Variable declaration.
 p.push('10); // Insert 10 to the queue, top=10
 p.push('30); // Insert 30 to the queue, top=30
 p.push('20); // Insert 20 to the queue, top=20
 cout << "The number of available elements to 'p': " << p.size() << endl;
 while(!p.empty())
 {
     std::cout << p.top() << std::endl; 
     p.pop();
 }
 return 0;
}

In the above code, we created a priority queue and inserted three elements, namely10,30,20. After inserting these elements, we use a while loop to display all elements of the priority queue.

Output Result

The number of available elements to 'p' is3
30
20
10

Let's see another example of the priority queue.

#include <iostream>
#include<queue>
using namespace std;
int main()
{
   priority_queue<int> p;  //Declaration of priority queue
   priority_queue<int> q;  //Declaration of priority queue
   p.push('1); // Insert '1to p.
   p.push('2); // Insert '2to p.
   p.push('3); // Insert '3to p.
   p.push('4); // Insert '4to p.
   q.push('5); // Insert '5to q.
   q.push('6); // Insert '6to q.
   q.push('7); // Insert '7to q.
   q.push('8); // Insert '8to q.
   p.swap(q);
   std::cout << "The elements of queue p are: " << std::endl;
   while(!p.empty())
   {
      std::cout << p.top() << std::endl;
       p.pop();
   }
   std::cout << "The priority queue element is: " << std::endl;
    while(!q.empty())
   {
      std::cout << q.top() << std::endl;
       q.pop();
   }
    return 0;
}

In the code above, we declared two priority queues, namely 'p' and 'q'. We inserted four elements into the 'p' priority queue and four elements into the 'q' priority queue. After inserting the elements, we used the swap() function to exchange the elements of the 'p' queue with the 'q' queue.

Output Result

The elements of the priority queue 'p' are:                                                                                                             
8                                                                                                                               
7                                                                                                                               
6                                                                                                                               
5                                                                                                                               
The elements of the priority queue 'q' are:                                                                                                             
4                                                                                                                               
3                                                                                                                               
2                                                                                                                               
1