DigestCPP

Lets Understand With Example

  • Home
  • Design Principal
  • Design Patterns
  • C++ 11 Features
  • C++11 Multithreading
  • Contact Us

std::priority_queue c++

Priority Queue:

This container comes under derived or adaptor container, this follow priority value (first element out will have highest priority). It has similar interface of queue.

Header File:

#include<queue>

Key Points:

  • It is used to store the data, we can store any number of element and remove from it.
  • Internally its implemented as vector we can change to other container.
  • push(): insert the element.
  • pop(): remove the element.
  • top(): return the top element BUT will not remove the element.
  • size(): return the size / number of element in it.
  • empty(): return true if queue is empty otherwise false.

How to add element in priority queue:

Source code of std::priority queue with example:

/*
Program: STL Priority Queue
Author: Alpha Master
Date: 20 September 2021
*/

//Header File
#include<iostream>
#include<queue>

int main()
{
    std::cout<<"STL Priority Queue"<<std::endl;

   //Create empty priority queue object
    std::priority_queue<int> pq;
    std::cout<<"size:"<<pq.size()<<std::endl;
 
    //Add element
    pq.push(1);
    pq.push(2);
    pq.push(3);
    pq.push(4);
    pq.push(5);
    std::cout<<"size:"<<pq.size()<<std::endl;

    //Top element
    std::cout<<"Execute Top command:";
    std::cout<<pq.top()<<std::endl;

    //Remove element
    std::cout<<"Execute pop command"<<std::endl;
    pq.pop();

    //Top element
    std::cout<<"Execute Top command:";
    std::cout<<pq.top()<<std::endl;


    std::cout<<"Display the complete"<<std::endl;
    //Display the element
    while(!pq.empty())
    {
        std::cout<<pq.top()<<" ";
        pq.pop();
    }
    std::cout<<std::endl;
    
    return 0;
}

Output:

STL Priority Queue
size:0
size:5
Execute Top command:5
Execute pop command
Execute Top command:4
Display the complete
4 3 2 1 

 

Second Scenario:

Priority Queue with user defined comparator. Use of decltype.

source code:

/*
Program: Priority Queue 
Author: Alpha Master
Date: 15 Nov 2021
*/

//Header File
#include<iostream>
#include<queue>

typedef std::pair<std::string, int> pi;

int main()
{
	std::cout<<"Priority Queue"<<std::endl;
	
	//pair
	pi p1 = std::make_pair("ram", 4);
	
	pi p2 = std::make_pair("shyam", 3);
        pi p3 = std::make_pair("abshyam", 2);
        pi p4 = std::make_pair("trshyam", 1);

	//min heap
	//greater
	auto a = [](pi a, pi b) ->bool { if(a.second > b.second){ return true;} else {return false;}};

	//priority queue
	std::priority_queue<pi, std::vector<pi>, decltype(a) > q(a);
	q.push(p1);
        q.push(p2);
        q.push(p3);
        q.push(p4);

	//display
	while(!(q.empty()))
	{
		std::cout<<q.top().second<<std::endl;
		q.pop();
	}

    	return 0;
}

Output:

Priority Queue
1
2
3
4

Primary Sidebar




DigestCPP © 2023. All rights reserved.

    About Privacy Policy Terms and Conditions Contact Us Disclaimer