c + + priority_queue

Posted by neon on Sun, 13 Feb 2022 06:44:16 +0100

The priority queue in C + + is a derived container in STL, which only considers the highest priority elements. The queue follows the FIFO policy, and the priority queue pops up the elements according to the priority, that is, the element with the highest priority pops up first.

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

  • In a priority queue, each element in the queue is associated with a 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, while the queue follows the FIFO (first in first out) policy, which means that the first inserted element will be deleted first.

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

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

Syntax of priority queue

priority_queue<int> variable_name;

Let's look at priority queues through a simple example.

In the above figure, we insert elements by 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 an element from the queue, the element with the highest priority will be deleted first.

Member function of priority queue

functiondescribe
push()It inserts the new element into the priority queue.
pop()It removes the highest priority element from the queue.
top()This function addresses the topmost element of the priority queue.
size()Returns the size of the priority queue.
empty()It verifies that the queue is empty. Based on validation, 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.

Examples

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

In the above code, we create a priority queue and insert three elements into it, namely 10, 30 and 20. After inserting these elements, we use the while loop to display all the elements of the priority queue.

Output results

Number of available elements reach 'p' :3
30
20
10

Let's look at another example of a priority queue.

Examples

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

In the above code, we declare two priority queues, 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 use the swap() function to swap the elements of the 'p' queue with the 'q' queue.

Output results

p The priority queue element is :   

8                                                                                                                               
7                                                                                                                               
6                                                                                                                               
5     


q The priority queue element is :      

4                                                                                                                               
3                                                                                                                               
2                                                                                                                               
1                                                                                    
                                                                                      

 

Topics: C++ Back-end