C++ STL Priority Queue – std::priority_queue

In this instructional exercise you will find out about STL need a line in C++ i.e std::priority_queue and all capacities relevant on it.

sexually transmitted disease:: priority_queue is a compartment connector. This is practically same as the line holder connector.

i.e this additionally fills in as first in first out (FIFO). Components consistently embedded at front position and cancellation additionally done from the front position.

Be that as it may, just contrast is components in the need line has some need. In this need line, the component which is at the top position has the most noteworthy need.

To utilize need line we just incorporate the line header document. There is no unique need line header document.

We incorporate line header record however to pick up the property of need to the components we announce as priority_queue. See underneath for all the more understanding.

C++ STL Priority Queue – std::priority_queue

#include <queue> // this is enough to work with priority queue. But while declaring do

priority_queue need line name;

The capacities related with need line are:

drive (component): To embed a component into the need line we use push activity.

fly(): To expel component from need line we use pop activity.

size(): To know the size of the need line we use size work. It returns a number of components that are available in need line.

top(): To get the main top component we use top work. It restores the most need component in need line.

void(): void is a Boolean capacity which returns genuine if the need line is vacant, generally returns bogus if need line isn’t unfilled.

swap(): If there are two need lines with swap activity we can trade all components from need queue1 to need queue2 and the other way around.

Here requirements are both must contain the same information sort of components. Be that as it may, the two sizes need not be equivalent.

Model program to show every single above work:

#include <iostream>
#include <queue>
 
using namespace std;
 
void display (priority_queue <int> pq)
{
    priority_queue <int> prq = pq;
 
    while (!prq.empty())
    {
        cout << prq.top() << " ";
        prq.pop();
    }
    cout << endl;
}
 
int main ()
{
    priority_queue <int> prq;
 
    for (int i=0; i<5; i++){
		prq.push(i+1);
	}
 
    cout << "the elements in priority queue are " << endl;
	display (prq);
 
	cout << "size of the priority queue is ";
    cout << prq.size() << endl;
    cout << "top element in priority queue is " ;
	cout << prq.top() << endl;
 
    cout << "performing one pop operation..." << endl;
    prq.pop ();
	cout << "result after pop operation ";
    display (prq);
 
	// checking whether priority queue is empty or not
	if (prq.empty()){
		cout << "priority queue is empty" << endl;
	}
	else{
		cout << "priority queue is not empty " << endl;
	}
	
	// below code for swap operation
	priority_queue <int> prq2; //creating new priority queue
	for (int i=0; i<5; i++){
		prq2.push (i*10);
	}
 
	cout << endl << "Priority queue 1 elements before swapping are " << endl;
	display(prq);
 
	cout << endl << "Priority queue 2 elements before swapping are " << endl;
	display(prq2);
	prq.swap(prq2);
 
	cout << endl << "Priority queue 1 elements after swapping are " << endl;
	display(prq);
 
	cout << endl << "Priority queue 2 elements after swapping are " << endl;
	display(prq2);
 
    return 0;
}

Output

the elements in priority queue are
5 4 3 2 1
size of the priority queue is 5
top element in priority queue is 5
performing one pop operation…
result after pop operation 4 3 2 1
priority queue is not empty

Priority queue 1 elements before swapping are
4 3 2 1

Priority queue 2 elements before swapping are
40 30 20 10 0

Priority queue 1 elements after swapping are
40 30 20 10 0

Priority queue 2 elements after swapping are
4 3 2 1

Leave a Comment

error: Alert: Content is protected!!