Skip to main content

C++ STL Priority Queue – std::priority_queue

In this tutorial you will learn about STL priority queue in C++ i.e std::priority_queue and all functions applicable on it.

std:: priority_queue is a container adaptor. This is almost same as queue container adaptor. i.e this also works as first in first out (FIFO). Elements always inserted at front position and deletion also done from front position. But only difference is elements in the priority queue has some priority. In this priority queue the element which is at top position has highest priority.

To use priority queue we simply include queue header file. There is no special priority queue header file. We include queue header file but to gain property of priority to the elements we declare as priority_queue. See below for 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 <data type> priority queue name;

The functions associated with priority queue are:

push (element): To insert element into priority queue we use push operation.

pop(): To remove element from priority queue we use pop operation.

size(): To know the size of the priority queue we use size function. It returns number of elements that are present in priority queue.

top(): To get the first top element we use top function. It returns the most priority element in priority queue.

empty(): empty is a Boolean function which returns true if the priority queue is empty, otherwise returns false if priority queue is not empty.

swap():  If there are two priority queues with swap operation we can exchange all elements from priority queue1 to priority queue2 and vice versa. Here constraints are both must contain same data type of elements. But both sizes need not be equal.

Example program to show all above functions:

#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

The post C++ STL Priority Queue – std::priority_queue appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2017/10/stl-priority-queue.html

Comments

Popular posts from this blog

Rail Fence Cipher Program in C and C++[Encryption & Decryption]

Here you will get rail fence cipher program in C and C++ for encryption and decryption. It is a kind of transposition cipher which is also known as zigzag cipher. Below is an example. Here Key = 3. For encryption we write the message diagonally in zigzag form in a matrix having total rows = key and total columns = message length. Then read the matrix row wise horizontally to get encrypted message. Rail Fence Cipher Program in C #include<stdio.h> #include<string.h> void encryptMsg(char msg[], int key){ int msgLen = strlen(msg), i, j, k = -1, row = 0, col = 0; char railMatrix[key][msgLen]; for(i = 0; i < key; ++i) for(j = 0; j < msgLen; ++j) railMatrix[i][j] = '\n'; for(i = 0; i < msgLen; ++i){ railMatrix[row][col++] = msg[i]; if(row == 0 || row == key-1) k= k * (-1); row = row + k; } printf("\nEncrypted Message: "); for(i = 0; i < key; ++i) f...

Data Encryption Standard (DES) Algorithm

Data Encryption Standard is a symmetric-key algorithm for the encrypting the data. It comes under block cipher algorithm which follows Feistel structure. Here is the block diagram of Data Encryption Standard. Fig1: DES Algorithm Block Diagram [Image Source: Cryptography and Network Security Principles and Practices 4 th Ed by William Stallings] Explanation for above diagram: Each character of plain text converted into binary format. Every time we take 64 bits from that and give as input to DES algorithm, then it processed through 16 rounds and then converted to cipher text. Initial Permutation: 64 bit plain text goes under initial permutation and then given to round 1. Since initial permutation step receiving 64 bits, it contains an 1×64 matrix which contains numbers from 1 to 64 but in shuffled order. After that, we arrange our original 64 bit text in the order mentioned in that matrix. [You can see the matrix in below code] After initial permutation, 64 bit text passed throug...

Accessibility Insights for the Web and Windows makes accessibility even easier

I recently stumbled upon https://accessibilityinsights.io . There's both a Chrome/ Edge extension and a Windows app, both designed to make it easier to find and fix accessibility issues in your websites and apps. The GitHub for the Accessibility Insights extension for the web is at https://github.com/Microsoft/accessibility-insights-web and they have three trains you can get on: Canary (released continuously) Insider (on feature completion) Production (after validation in Insider) It builds on top of the Deque Axe core engine with a really fresh UI. The "FastPass" found these issues with my podcast site in seconds - which kind of makes me feel bad, but at least I know what's wrong! However, the most impressive visualization in my opinion was the Tab Stop test! See below how it draws clear numbered line segments as you Tab from element. This is a brilliant way to understand exactly how someone without a mouse would move through your site. I can easily s...