Priority Queue in STL C++

About Priority Queue in STL :

Priority Queue in STL is defined as the adapter container used to store elements in the particular order either in increasing or decreasing  depend on the implementations.

priority queue in stl

Syntax of Priority Queue :

priority_queue< data_type> priority_queue_name;

The first element of a priority queue is either the largest or the smallest of all the elements in the queue, and the elements are arranged in a non increasing order . Priority queues are a type of container adapter. However, the highest element is always the default in the C++ STL. We may alternatively put the smallest component at the top instead. Priority queues are constructed using an internal array or vector and are created on top of the heap to its maximum height.

Functions in Priority Queue :

 FunctionWorking
push()insert an element at back end of the priority queue.
top()give the top element of the priority queue.
size()give the number of elements present in the priority queue
empty()tells whether the priority queue is empty or not.
pop()

remove the top elements from priority queue.

Example of  Priority Queue in STL :

Run
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
    priority_queue< int>pq;
    pq.push(34);
    pq.push(20);
    pq.push(4);
    pq.push(1);
    pq.push(50);
    cout<<"The element at the top of the priority queue : "<< pq.top()<< endl;
    pq.pop();
    cout<<"The element at the top of the priority queue after remove one element : "<< pq.top();

    return 0;
}

Output :

The element at the top of the priority queue : 50
The element at the top of the priority queue after remove one element : 34

In the above program, we take the  priority queue(by default it is maximum priority queue) pq, push the elements 34, 20,4, 1, 50 in it and display the top element of the priority queue which is always the greatest of all the numbers.

Example of Priority Queue in STL:

Run
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
    priority_queue< int,vector< int>, greater< int > >pq;
    pq.push(34);
    pq.push(20);
    pq.push(4);
    pq.push(1);
    pq.push(50);
    cout<<"The element at the top of the priority queue : "<< pq.top()<< endl;
    pq.pop();
    cout<<"The element at the top of the priority queue after remove one element : "<< pq.top();

    return 0;
}

Output :

The element at the top of the priority queue : 1
The element at the top of the priority queue after remove one element : 4

In the above program, we take the minimum priority queue pq, push the elements 34, 20,4, 1, 50 in it and display the top element of the priority queue which is always the smallest of all the numbers.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription