
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Priority Queue in C++ Standard Template Library (STL)
Priority queue is an abstract data type for storing a collection of prioritized elements that supports insertion and deletion of an element based upon their priorities, that is, the element with first priority can be removed at any time. The priority queue doesn’t stores elements in linear fashion with respect to their locations like in Stacks, Queues, List, etc. The priority queue ADT(abstract data type) stores elements based upon their priorities.
Priority Queue supports the following functions −
Size() − it is used to calculate the size of the priority queue as it returns the number of elements in it.
Empty() − it return true if the Priority Queue is empty and false otherwise
Insert(element) − used to insert the new element into a Priority Queue
Min() − it return the element with the smallest associated key value and display error message if Priority Queue is empty.
removeMin()− it removes the element referenced by the min() function.
Given below is a table that shows the effect of operations on a priority queue
Start Step 1-> Declare function to display the elements in a Priority Queue void display(priority_queue <int> Pq) declare and set priority_queue <int> que = Pq Loop While (!que.empty()) call que.top() call que.pop() End Step 2-> In main() Create object of priority_queue <int> Pq Call push() to insert element in a priority queue as Pq.push(1) Call display(Pq) Call to check the size of a priority queue Pq.size() Call to display the top element of a priority queue Pq.top() Call to remove the elements of a priority queue Pq.pop() Call display(Pq) Stop
Example
#include <iostream> #include <queue> using namespace std; void display(priority_queue <int> Pq) { priority_queue <int> que = Pq; while (!que.empty()) { cout << '\t' << que.top(); que.pop(); } //cout << '\n'; } int main () { priority_queue <int> Pq; Pq.push(1); Pq.push(3); Pq.push(5); Pq.push(7); Pq.push(9); cout << "The priority queue is : "; display(Pq); cout << "\nPrioriy queue size using size() : " << Pq.size(); cout << "\nFirst element of priority queue using top(): " << Pq.top(); cout << "\nremoving element using pop() : "; Pq.pop(); display(Pq); return 0; }
Output
The priority queue is : 9 7 5 3 1 Prioriy queue size using size() : 5 First element of priority queue using top(): 9 removing element using pop() : 7 5 3 1
- Related Articles
- The C++ Standard Template Library (STL)
- Sort in C++ Standard Template Library (STL)
- Multiset in C++ Standard Template Library (STL)
- Pair in C++ Standard Template Library (STL)
- Binary Search in C++ Standard Template Library (STL)
- STL Priority Queue for Structure or Class in C++
- Dequeue and Priority Queue in C++
- Priority Queue Introduction in C/C++\n
- What's the difference between "STL" and "C++ Standard Library"?
- Priority Queue using Linked List in C
- Double ended priority queue in C++ Program
- Should we declare it as Queue or Priority Queue while using Priority Queue in Java?
- C++ Program to Implement Priority Queue
- queue::front() and queue::back() in C++ STL
- queue::empty() and queue::size() in C++ STL
