🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 34 (from laksa025)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

📄
INDEXABLE
CRAWLED
3 hours ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://www.mygreatlearning.com/blog/priority-queue-in-cpp/
Last Crawled2026-04-06 20:46:38 (3 hours ago)
First Indexed2021-02-28 18:33:09 (5 years ago)
HTTP Status Code200
Meta TitlePriority Queue in C++ | Everything you need to know about Priority Queues
Meta DescriptionA priority queue in c++ is a type of container adapter, which processes only the highest priority element in a queue and sorts accordingly.
Meta Canonicalnull
Boilerpipe Text
Table of contents What is a Priority Queue in C++? C++ Priority Queue Methods Conclusion Contents What is Priority Queue in C++? Difference between Priority Queue and Queue Syntax of Priority Queue Syntax to create min-heap for Priority Queue How does c++ Priority Queue work? Inserting elements Accessing ele m ents Deleting elements   C++ Priority Queue Methods   Simple programs of Priority Queue   Conclusion Summarize this article with ChatGPT Get key takeaways & ask questions What is a Priority Queue in C++? A  priority queue in c++ is a type of container adapter, which processes only the highest priority element, i.e. the first element will be the maximum of all elements in the queue, and elements are in decreasing order. C++ Programming Course Master key C++ programming concepts like variables, functions, OOP, and control structures. Build real-world projects such as a banking system and grade management tool. Beginner to Advanced Level 8.1 hrs Start Free Trial Difference between a queue and a priority queue: Priority Queue Queue Priority Queue container processes the element with the highest priority, whereas no priority exists in a queue. The queue follows First-in-First-out (FIFO) rule, but in the priority queue highest priority element will be deleted first. If more than one element exists with the same priority, then, in this case, the order of queue will be taken. Syntax of Priority Queue:   priority_queue&lt;int> variableName;    Note :  By default, C++ creates a max-heap for the priority queue. Syntax to create min-heap for the Priority Queue:   priority_queue &lt;int, vector&lt;int>, greater&lt;int>> q;  Where vector<int> is a STL container and greater<int> is comparator class. How does c++ Priority Queue work? Priority Queue considers only the highest priority element. Inserting elements in a Priority Queue: /* Program to insert elements in a queue*/ #include&lt;iostream> #include&lt;queue> //Header-file for queue using namespace std; int main() { priority_queue&lt;int> p1; p1.push(35); // inserting element in a queue p1.push(40); p1.push(95); while (!p1.empty()) { cout &lt;&lt; ' ' &lt;&lt; p1.top(); //printing elements of queue p1.pop(); } } Accessing elements in a Priority Queue: /* Program to access an element of highest priority */ #include&lt;iostream> #include&lt;queue> //Header-file for queue using namespace std; int main() { priority_queue&lt;int> p1; p1.push(35); p1.push(40); p1.push(95); p1.push(25); cout&lt;&lt;p1.top(); //fetch element of highest priority(maximum element) i.e 95 } Deleting elements in a Priority Queue: /* Program to delete elements in a queue*/ #include&lt;iostream> #include&lt;queue> //Header-file for queue using namespace std; int main() { priority_queue&lt;int> p1; p1.push(35); p1.push(40); p1.push(95); p1.push(20); // queue : 95 40 35 20 p1.pop(); // queue : 40 35 20 p1.pop(); // queue : 35 20 while (!p1.empty()) { cout &lt;&lt; ' ' &lt;&lt; p1.top(); p1.pop(); } } C++ Priority Queue Methods Following are the C++ Priority Queue Methods : Methods Description empty() This method checks whether the priority_queue container is empty or not. If it is empty, return true, else false. It does not take any parameters. syntax  :   p1.empty()                       // p1 is priority_queue object size() This method gives the number of elements in the priority queue container. It returns the size in an integer. It does not take any parameter. syntax :   p2.size()                         // p2 is priority_queue object push() This method inserts the element into the queue. Firstly, the element is added to the end of the queue, and simultaneously elements reorder themselves with priority. It takes value in the parameter. syntax :  p3.push(value)            //value to be inserted pop() This method  delete the top element (highest priority) from the priority_queue. It does not take any parameter. syntax :  p3.pop()           // p3 is priority_queue object top() This method gives the top element from the priority queue container. It does not take any parameter. syntax :  p3.top()   swap() This method swaps the elements of a priority_queue with another priority_queue of the same size and type. It takes the priority queue in a parameter whose values need to be swapped. syntax :  p3.swap(p1)    emplace() This method adds a new element in a container at the top of the priority queue. It takes value in a parameter. syntax :  p3.emplace(value)    Lets see the above methods  with a  simple program : A Program to show size and empty method: #include&lt;iostream> #include&lt;queue> //Header-file for queue using namespace std; int main() { priority_queue&lt;int> q1; q1.push(25); //add 25 in a queue q1.push(15); q1.push(30); q1.push(76); q1.push(85); // queue : 85 76 30 25 15 cout&lt;&lt;" Number of elements in a priority queue : "&lt;&lt;q1.size()&lt;&lt;endl; cout&lt;&lt;" The top element is : "&lt;&lt;q1.top()&lt;&lt;endl; //return the maximum element from the queue; q1.pop(); //remove the highest priority element(MAX element) from the queue; // queue : 76 30 25 15 q1.push(55); q1.push(89); q1.push(35); cout&lt;&lt;" After Inserting : "&lt;&lt;endl; //queue : 89 76 55 35 30 25 15 while(!q1.empty()) { cout&lt;&lt;" "&lt;&lt;q1.top()&lt;&lt;" "; q1.pop(); } } Program of swap method :  #include &lt;iostream> #include &lt;queue> using namespace std; int main() { priority_queue&lt;int> p1,q1; //pushing value in p1. p1.push(28); p1.push(69); p1.push(36); p1.push(23); p1.push(14); //pushing value in q1. q1.push(10); q1.push(80); q1.push(32); q1.push(20); q1.push(15); p1.swap(q1); cout&lt;&lt; "elements in p1 : "; while(!p1.empty()) { cout&lt;&lt;" "&lt;&lt;p1.top() &lt;&lt;' '; p1.pop(); } cout&lt;&lt; '\n'; cout&lt;&lt; "elements in m1 : "; while(!q1.empty()) { cout&lt;&lt;" "&lt;&lt;q1.top() &lt;&lt; ' '; q1.pop(); } return 0; } A Program of embrace method : #include&lt;iostream> #include&lt;queue> //Header-file for queue #include&lt;string> using namespace std; int main() { priority_queue&lt;string> q1; q1.emplace("Ankit"); q1.emplace("Ved"); q1.emplace("Nikita"); q1.emplace("Shaurya"); q1.emplace("Anokhi"); // queue: Ved Shaurya Nikita Anokhi Ankit t while (!q1.empty()) { cout&lt;&lt;" "&lt;&lt;q1.top() &lt;&lt; " "; q1.pop(); } return 0; } C++ Program to implement min-heap :  #include&lt;iostream> #include&lt;queue> //Header-file for queue using namespace std; int main() { // creates a min heap priority_queue &lt;int, vector&lt;int>, greater&lt;int> > p1; p1.push(55); p1.push(1); p1.push(76); p1.push(39); p1.push(23); p1.push(49); p1.push(32); while (!p1.empty()) { cout &lt;&lt;" "&lt;&lt; p1.top() &lt;&lt; " "; p1.pop(); } return 0; } Conclusion Therefore, Priority Queue is a container that stores elements with priority. Unlike queues, which insert or delete the element based on the FIFO rule, in Priority Queue, elements are removed based on priority. The element with the highest priority is the first one to be removed from the queue. Priority queue supports three operations: is_empty to check if there is no element in the queue, insert_with_priority , to add an element with priority, and Pull_highest_priority_element , to fetch element of highest priority in the queue and show it. The importance of the priority queue is to process the elements based on priority. This brings us to the end of the blog on the concept of Priority Queue in C++. We hope that you found this comprehensive and helpful and were able to gain the required knowledge. If you wish to up-skill and learn more such concepts, you can  check out the pool of Free online courses on Great Learning Academy. Also, if you are preparing for Interviews, check out these  Interview Questions for C++  to ace it like a pro.
Markdown
[Create ATS-friendly Resume for Free Use our Free AI Resume Builder](https://www.mygreatlearning.com/academy/pro/resume-builder?utm_source=blog) [Skip to content](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#content) [![great learning](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20115%2036'%3E%3C/svg%3E)![great learning](https://d1vwxdpzbgdqj.cloudfront.net/s3-public-images/learning-partners/greatlearning-brand.svg)](https://www.mygreatlearning.com/) [Blog](https://www.mygreatlearning.com/blog/) - [Browse Topics](https://www.mygreatlearning.com/blog/) - [AI and Machine Learning](https://www.mygreatlearning.com/blog/artificial-intelligence/) - [Data Science and Analytics](https://www.mygreatlearning.com/blog/data-science/) - [Cloud Computing](https://www.mygreatlearning.com/blog/cloud-computing/) - [Cybersecurity](https://www.mygreatlearning.com/blog/cybersecurity/) - [IT/Software Development](https://www.mygreatlearning.com/blog/software/) - [Career Development](https://www.mygreatlearning.com/blog/career/) - [Digital Marketing](https://www.mygreatlearning.com/blog/digital-marketing/) - [Business Management](https://www.mygreatlearning.com/blog/businessmanagement/) - [UI/UX Design](https://www.mygreatlearning.com/blog/design-thinking/) - [Study Abroad](https://www.mygreatlearning.com/blog/study-abroad/) - [Research and Studies](https://www.mygreatlearning.com/blog/research-and-studies/) - [PG Courses](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/) - [Artificial Intelligence Courses](https://www.mygreatlearning.com/artificial-intelligence/courses) - [Data Science Courses](https://www.mygreatlearning.com/data-science/courses) - [Gen AI Courses](https://www.mygreatlearning.com/gen-ai/courses) - [Management Courses](https://www.mygreatlearning.com/management/courses) - [MBA Courses](https://www.mygreatlearning.com/degrees/mba-courses) - [Cloud Computing Courses](https://www.mygreatlearning.com/cloud-computing/courses) - [Cybersecurity Courses](https://www.mygreatlearning.com/cyber-security/courses) - [Software Engineering Courses](https://www.mygreatlearning.com/software-engineering/courses) - [Design Courses](https://www.mygreatlearning.com/design/courses) - [Master Degree Courses](https://www.mygreatlearning.com/degrees/masters-courses) - [Post Graduate Courses](https://www.mygreatlearning.com/pg-courses) - [Professional Courses](https://www.mygreatlearning.com/professional-certificates) - [Master Courses](https://www.mygreatlearning.com/degrees/masters-courses) - [Free Courses](https://www.mygreatlearning.com/academy) - [Artificial Intelligence Free Courses](https://www.mygreatlearning.com/ai/free-courses) - [Machine Learning Free Courses](https://www.mygreatlearning.com/machine-learning/free-courses) - [Data Science Free Courses](https://www.mygreatlearning.com/data-science/free-courses) - [Cyber Security Free Courses](https://www.mygreatlearning.com/cybersecurity/free-courses) - [Cloud Computing Free Courses](https://www.mygreatlearning.com/cloud-computing/free-courses) - [IT & Software Free Courses](https://www.mygreatlearning.com/software/free-courses) - [Interview Preparation Free Courses](https://www.mygreatlearning.com/interview-preparation/free-courses) - [Digital Marketing Free Courses](https://www.mygreatlearning.com/digital-marketing/free-courses) - [Management Free Courses](https://www.mygreatlearning.com/management/free-courses) - [Resources & Tools]() - [Free Resume Builder](https://www.mygreatlearning.com/academy/pro/resume-builder) - [GenAI Academy](https://www.greatlearning.ai/) - [Tools and Compilers](https://www.mygreatlearning.com/blog/tools-and-compilers/) - [Coding Practice](https://www.mygreatlearning.com/blog/exercises/) - [Quizzes](https://www.mygreatlearning.com/blog/quizzes/) - [Career Options](https://www.mygreatlearning.com/blog/careers-and-roadmap/) - [Project Ideas](https://www.mygreatlearning.com/blog/project-ideas/) - [Interview Questions](https://www.mygreatlearning.com/blog/interview-questions/) - [Follow us on Google](https://www.google.com/preferences/source?q=mygreatlearning.com) [![great learning](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20115%2036'%3E%3C/svg%3E)![great learning](https://d1vwxdpzbgdqj.cloudfront.net/s3-public-images/learning-partners/greatlearning-brand.svg)](https://www.mygreatlearning.com/) [Blog](https://www.mygreatlearning.com/blog/) - [Coding Practice](https://www.mygreatlearning.com/blog/exercises/) - [Compilers](https://www.mygreatlearning.com/blog/tools-and-compilers/) - [Career Guide](https://www.mygreatlearning.com/blog/careers-and-roadmap/) - [Quizzes](https://www.mygreatlearning.com/blog/quizzes/) - [Project Ideas](https://www.mygreatlearning.com/blog/project-ideas/) - [Interview Questions](https://www.mygreatlearning.com/blog/interview-questions/) [Great Learning](https://www.mygreatlearning.com/) ![](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%207%2011'%3E%3C/svg%3E) ![](https://www.mygreatlearning.com/blog-assets/img/chev-right.svg) [Blog](https://www.mygreatlearning.com/blog/) ![](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%207%2011'%3E%3C/svg%3E) ![](https://www.mygreatlearning.com/blog-assets/img/chev-right.svg) [IT/Software Development](https://www.mygreatlearning.com/blog/software/) ![](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%207%2011'%3E%3C/svg%3E) ![](https://www.mygreatlearning.com/blog-assets/img/chev-right.svg) Priority Queue in C++ Table of Contents - - - [What is a Priority Queue in C++?](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#what-is-a-priority-queue-in-c) - [C++ Priority Queue Methods](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#c-priority-queue-methods) - [Conclusion](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#conclusion) # Priority Queue in C++ By [Great Learning Editorial Team](https://www.mygreatlearning.com/blog/author/greatlearning/ "Posts by Great Learning Editorial Team") Updated on Sep 3, 2024 ![Priority Queue in C++](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%201000%20667'%3E%3C/svg%3E) ![Priority Queue in C++](https://www.mygreatlearning.com/blog/wp-content/uploads/2021/02/shutterstock_1183376491.jpg) Table of contents - - - [What is a Priority Queue in C++?](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#what-is-a-priority-queue-in-c) - [C++ Priority Queue Methods](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#c-priority-queue-methods) - [Conclusion](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#conclusion) ### **Contents** 1. [What is Priority Queue in C++?](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#what-is-a-priority-queue-in-C++) 2. [Difference between Priority Queue and Queue](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#difference-between-a-queue-and-priority-queue) 3. [Syntax of Priority Queue](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#syntax) 4. [Syntax to create min-heap for Priority Queue](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#min-heap) 5. [How does c++ Priority Queue work?](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#how-does-c++-priority-queue-work) - [Inserting elements](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#inserting-elements) - [Accessing ele](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#accessing%20elements)[m](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#accessing-elements)[ents](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#accessing%20elements) - [Deleting elements](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#deleting-elements) 6. [C++ Priority Queue Methods](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#C++-priority-queue-methods) 7. [Simple programs of Priority Queue](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#simple-programs-of-priority-queue) 8. [Conclusion](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#conclusion) Summarize this article with ChatGPT Get key takeaways & ask questions ## **What is a Priority Queue in C++?** A priority queue in c++ is a type of container adapter, which processes only the highest priority element, i.e. the first element will be the maximum of all elements in the queue, and elements are in decreasing order. Academy Pro [C++ Programming Course](https://www.mygreatlearning.com/academy/premium/learn-c-programming-for-beginners-to-advanced) Master key C++ programming concepts like variables, functions, OOP, and control structures. Build real-world projects such as a banking system and grade management tool. Beginner to Advanced Level 8\.1 hrs [Start Free Trial](https://www.mygreatlearning.com/academy/premium/learn-c-programming-for-beginners-to-advanced) ### **Difference between a queue and a priority queue:** | | | |---|---| | Priority Queue | Queue | | Priority Queue container processes the element with the highest priority, whereas no priority exists in a queue. | The queue follows First-in-First-out (FIFO) rule, but in the priority queue highest priority element will be deleted first. | - If more than one element exists with the same priority, then, in this case, the order of queue will be taken. ### **Syntax of Priority Queue:** ```   priority_queue&lt;int> variableName;    ``` **Note**: By default, C++ creates a max-heap for the priority queue. ### **Syntax to create min-heap for the Priority Queue:** ```   priority_queue &lt;int, vector&lt;int>, greater&lt;int>> q;  ``` Where vector\<int\> is a STL container and greater\<int\> is comparator class. ### **How does c++ Priority Queue work?** Priority Queue considers only the highest priority element. ### Inserting elements in a Priority Queue: ``` /* Program to insert elements in a queue*/ #include&lt;iostream> #include&lt;queue> //Header-file for queue using namespace std; int main() { priority_queue&lt;int> p1; p1.push(35); // inserting element in a queue p1.push(40); p1.push(95); while (!p1.empty()) { cout &lt;&lt; ' ' &lt;&lt; p1.top(); //printing elements of queue p1.pop(); } } ``` ### Accessing elements in a Priority Queue: ``` /* Program to access an element of highest priority */ #include&lt;iostream> #include&lt;queue> //Header-file for queue using namespace std; int main() { priority_queue&lt;int> p1; p1.push(35); p1.push(40); p1.push(95); p1.push(25); cout&lt;&lt;p1.top(); //fetch element of highest priority(maximum element) i.e 95 } ``` ### Deleting elements in a Priority Queue: ``` /* Program to delete elements in a queue*/ #include&lt;iostream> #include&lt;queue> //Header-file for queue using namespace std; int main() { priority_queue&lt;int> p1; p1.push(35); p1.push(40); p1.push(95); p1.push(20); // queue : 95 40 35 20 p1.pop(); // queue : 40 35 20 p1.pop(); // queue : 35 20 while (!p1.empty()) { cout &lt;&lt; ' ' &lt;&lt; p1.top(); p1.pop(); } } ``` ## Learn More C++ Concepts [OOPs Concepts in C++ Free Course](https://www.mygreatlearning.com/academy/learn-for-free/courses/oops-concepts-in-c) [C For Beginners Free Course](https://www.mygreatlearning.com/academy/learn-for-free/courses/c-for-beginners1) [Turbo C++ Free Course](https://www.mygreatlearning.com/academy/learn-for-free/courses/turbo-c) [C++ Tutorial Free Course](https://www.mygreatlearning.com/academy/learn-for-free/courses/c-tutorial) ## **C++ Priority Queue Methods** Following are the C++ Priority Queue Methods : | | | |---|---| | Methods | Description | | empty() | This method checks whether the priority\_queue container is empty or not. If it is empty, return true, else false. It does not take any parameters. syntax : p1.empty() // p1 is priority\_queue object | | size() | This method gives the number of elements in the priority queue container. It returns the size in an integer. It does not take any parameter. syntax : p2.size() *// p2 is priority\_queue object* | | push() | This method inserts the element into the queue. Firstly, the element is added to the end of the queue, and simultaneously elements reorder themselves with priority. It takes value in the parameter. syntax : p3.push(value) *//value to be inserted* | | pop() | This method delete the top element (highest priority) from the priority\_queue. It does not take any parameter. syntax : p3.pop() // p3 is priority\_queue object | | top() | This method gives the top element from the priority queue container. It does not take any parameter. syntax : p3.top() | | swap() | This method swaps the elements of a priority\_queue with another priority\_queue of the same size and type. It takes the priority queue in a parameter whose values need to be swapped. syntax : p3.swap(p1) | | emplace() | This method adds a new element in a container at the top of the priority queue. It takes value in a parameter. syntax : p3.emplace(value) | Lets see the above methods with a simple program : ### A **Program to show size and empty method:** ``` #include&lt;iostream> #include&lt;queue> //Header-file for queue using namespace std; int main() { priority_queue&lt;int> q1; q1.push(25); //add 25 in a queue q1.push(15); q1.push(30); q1.push(76); q1.push(85); // queue : 85 76 30 25 15 cout&lt;&lt;" Number of elements in a priority queue : "&lt;&lt;q1.size()&lt;&lt;endl; cout&lt;&lt;" The top element is : "&lt;&lt;q1.top()&lt;&lt;endl; //return the maximum element from the queue; q1.pop(); //remove the highest priority element(MAX element) from the queue; // queue : 76 30 25 15 q1.push(55); q1.push(89); q1.push(35); cout&lt;&lt;" After Inserting : "&lt;&lt;endl; //queue : 89 76 55 35 30 25 15 while(!q1.empty()) { cout&lt;&lt;" "&lt;&lt;q1.top()&lt;&lt;" "; q1.pop(); } } ``` ### **Program of swap method :** ``` #include &lt;iostream> #include &lt;queue> using namespace std; int main() { priority_queue&lt;int> p1,q1; //pushing value in p1. p1.push(28); p1.push(69); p1.push(36); p1.push(23); p1.push(14); //pushing value in q1. q1.push(10); q1.push(80); q1.push(32); q1.push(20); q1.push(15); p1.swap(q1); cout&lt;&lt; "elements in p1 : "; while(!p1.empty()) { cout&lt;&lt;" "&lt;&lt;p1.top() &lt;&lt;' '; p1.pop(); } cout&lt;&lt; '\n'; cout&lt;&lt; "elements in m1 : "; while(!q1.empty()) { cout&lt;&lt;" "&lt;&lt;q1.top() &lt;&lt; ' '; q1.pop(); } return 0; } ``` ### A **Program of embrace method :** ``` #include&lt;iostream> #include&lt;queue> //Header-file for queue #include&lt;string> using namespace std; int main() { priority_queue&lt;string> q1; q1.emplace("Ankit"); q1.emplace("Ved"); q1.emplace("Nikita"); q1.emplace("Shaurya"); q1.emplace("Anokhi"); // queue: Ved Shaurya Nikita Anokhi Ankit t while (!q1.empty()) { cout&lt;&lt;" "&lt;&lt;q1.top() &lt;&lt; " "; q1.pop(); } return 0; } ``` ### **C++ Program to implement min-heap :** ``` #include&lt;iostream> #include&lt;queue> //Header-file for queue using namespace std; int main() { // creates a min heap priority_queue &lt;int, vector&lt;int>, greater&lt;int> > p1; p1.push(55); p1.push(1); p1.push(76); p1.push(39); p1.push(23); p1.push(49); p1.push(32); while (!p1.empty()) { cout &lt;&lt;" "&lt;&lt; p1.top() &lt;&lt; " "; p1.pop(); } return 0; } ``` ## **Conclusion** Therefore, Priority Queue is a container that stores elements with priority. Unlike queues, which insert or delete the element based on the FIFO rule, in Priority Queue, elements are removed based on priority. The element with the highest priority is the first one to be removed from the queue. Priority queue supports three operations: **is\_empty** to check if there is no element in the queue,**insert\_with\_priority**, to add an element with priority, and **Pull\_highest\_priority\_element**, to fetch element of highest priority in the queue and show it. The importance of the priority queue is to process the elements based on priority. This brings us to the end of the blog on the concept of Priority Queue in C++. We hope that you found this comprehensive and helpful and were able to gain the required knowledge. If you wish to up-skill and learn more such concepts, you can [check out the pool of Free online courses on Great Learning Academy.](https://www.mygreatlearning.com/academy) Also, if you are preparing for Interviews, check out these [Interview Questions for C++](https://www.mygreatlearning.com/blog/cpp-interview-questions/) to ace it like a pro. ![Avatar photo](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20120%20120'%3E%3C/svg%3E) ![Avatar photo](https://www.mygreatlearning.com/blog/wp-content/uploads/2022/02/unnamed.webp) Great Learning Editorial Team The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields. Recommended for you [![android tutorial](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%201000%20667'%3E%3C/svg%3E)![android tutorial](https://www.mygreatlearning.com/blog/wp-content/uploads/2021/02/shutterstock_1137339098.jpg)](https://www.mygreatlearning.com/blog/android-tutorial/) [Android Tutorial - Everything You Need to Know Updated on Oct 25, 2022 2025](https://www.mygreatlearning.com/blog/android-tutorial/) [![jdbc tutorial](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%201254%20836'%3E%3C/svg%3E)![jdbc tutorial](https://www.mygreatlearning.com/blog/wp-content/uploads/2021/02/iStock-1280171152-1.jpg)](https://www.mygreatlearning.com/blog/jdbc-tutorial/) [JDBC Tutorial - What is JDBC and Examples of it Updated on Nov 18, 2024 5385](https://www.mygreatlearning.com/blog/jdbc-tutorial/) [![scala tutorial](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%201254%20837'%3E%3C/svg%3E)![scala tutorial](https://www.mygreatlearning.com/blog/wp-content/uploads/2021/02/iStock-1075599562.jpg)](https://www.mygreatlearning.com/blog/scala-tutorial/) [Scala Tutorial - Learn Scala Step-by-Step Guide Updated on Oct 24, 2024 19882](https://www.mygreatlearning.com/blog/scala-tutorial/) [![java virtual machine](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%201000%20812'%3E%3C/svg%3E)![java virtual machine](https://www.mygreatlearning.com/blog/wp-content/uploads/2021/03/shutterstock_304650200.jpg)](https://www.mygreatlearning.com/blog/java-virtual-machine/) [Java Virtual Machine Updated on Sep 3, 2024 5869](https://www.mygreatlearning.com/blog/java-virtual-machine/) [![strings in java](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%201000%20667'%3E%3C/svg%3E)![strings in java](https://www.mygreatlearning.com/blog/wp-content/uploads/2021/03/shutterstock_1177872616.jpg)](https://www.mygreatlearning.com/blog/strings-in-java/) [Strings in Java: Creation, Methods & Best Practices Updated on Jun 18, 2025 1656](https://www.mygreatlearning.com/blog/strings-in-java/) [![capstone project](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%201254%20836'%3E%3C/svg%3E)![capstone project](https://www.mygreatlearning.com/blog/wp-content/uploads/2021/03/iStock-1255236227.jpg)](https://www.mygreatlearning.com/blog/standard-template-library-in-c/) [Standard Template Library in C++ Updated on Sep 3, 2024 5711](https://www.mygreatlearning.com/blog/standard-template-library-in-c/) ### Free Courses - [Free Artificial Intelligence Course With Certificate](https://www.mygreatlearning.com/academy/learn-for-free/courses/introduction-to-artificial-intelligence) - [Free Prompt Engineering Course With Certificate](https://www.mygreatlearning.com/academy/learn-for-free/courses/prompt-engineering-for-chatgpt) - [Python for Machine Learning Free Course](https://www.mygreatlearning.com/academy/learn-for-free/courses/python-for-machine-learning) - [Data Science Foundations Free Course](https://www.mygreatlearning.com/academy/learn-for-free/courses/data-science-foundations) - [Deep Learning with Python Free Course](https://www.mygreatlearning.com/academy/learn-for-free/courses/deep-learning-with-python) - [Introduction to Cyber Security Free Course](https://www.mygreatlearning.com/academy/learn-for-free/courses/introduction-to-cyber-security) - [Free Digital Marketing Course](https://www.mygreatlearning.com/academy/learn-for-free/courses/introduction-to-digital-marketing) - [Java Programming Free Course](https://www.mygreatlearning.com/academy/learn-for-free/courses/java-programming) - [View More →](https://www.mygreatlearning.com/academy) ### PG & Professional Certificate Courses - [PG Courses](https://www.mygreatlearning.com/pg-courses) - [Professional Courses](https://www.mygreatlearning.com/professional-certificates) ### International Courses - [UT Austin: PG Program in Artificial Intelligence and Machine Learning](https://onlineexeced.mccombs.utexas.edu/online-ai-machine-learning-course) - [UT Austin: Artificial Intelligence PG Program for Leaders](https://onlineexeced.mccombs.utexas.edu/ai-for-business-leaders-course) - [UT Austin: PG Program in Data Science with Gen AI](https://onlineexeced.mccombs.utexas.edu/online-data-science-business-analytics-course) - [UT Austin: PG Program in Generative AI for Business Applications](https://onlineexeced.mccombs.utexas.edu/gen-ai-for-business-applications-online-course) - [UT Austin: Full Stack Developer Program](https://onlineexeced.mccombs.utexas.edu/online-full-stack-software-development-course) - [MIT: Applied AI and Data Science Program](https://professional-education-gl.mit.edu/mit-online-data-science-program) - [MIT IDSS Data Science and Machine Learning Course](https://idss-gl.mit.edu/mit-idss-data-science-machine-learning-online-program) - [MIT: No Code AI and Machine Learning](https://professionalonline2.mit.edu/no-code-artificial-intelligence-machine-learning-program) - [JHU: AI in Healthcare Program](https://online.lifelonglearning.jhu.edu/jhu-ai-in-healthcare-certificate-program) - [JHU: Certificate Program in Applied Generative AI](https://online.lifelonglearning.jhu.edu/jhu-certificate-program-applied-generative-ai) - [JHU: Certificate Program in AI Business Strategy](https://online.lifelonglearning.jhu.edu/jhu-certificate-program-ai-for-business-strategy) - [JHU: Certificate Program in Agentic AI](https://online.lifelonglearning.jhu.edu/jhu-certificate-program-agentic-ai) - [JHU: Professional Certificate in Cybersecurity](https://online.lifelonglearning.jhu.edu/jhu-cybersecurity-certificate-program) - [Great Lakes: PG Diploma in Management (Online)](https://www.greatlakes.edu.in/online/pgdm-online) ### Premium Courses - [Learn JavaScript](https://www.mygreatlearning.com/academy/premium/advanced-javascript-development) - [Power BI Training](https://www.mygreatlearning.com/academy/premium/data-visualization-with-powerbi) - [Learn SQL Programming](https://www.mygreatlearning.com/academy/premium/practical-sql-training) - [Data Analysis in Excel](https://www.mygreatlearning.com/academy/premium/master-data-analytics-in-excel) - [Digital Marketing Course](https://www.mygreatlearning.com/academy/premium/mastering-digital-marketing) - [Learn Python Programming](https://www.mygreatlearning.com/academy/premium/master-python-programming) - [Tableau for Data Visualization](https://www.mygreatlearning.com/academy/premium/tableau-data-visualization-essentials) ### Popular Courses - [Post Graduate Program in Data Science](https://www.mygreatlearning.com/pg-program-data-science-and-business-analytics-course) - [PGP In Artificial Intelligence And Machine Learning](https://www.mygreatlearning.com/pg-program-artificial-intelligence-course) - [PGP In Management](https://www.mygreatlearning.com/pg-program-management-executive) - [PGP In Cloud Computing](https://www.mygreatlearning.com/pg-program-cloud-computing-course) - [Software Engineering Course](https://www.mygreatlearning.com/software-engineering/courses) - [Digital Marketing Course](https://www.mygreatlearning.com/academy/premium/mastering-digital-marketing) - [View More →](https://www.mygreatlearning.com/) - [About Us](https://www.mygreatlearning.com/about-us) - [Contact Us](https://www.mygreatlearning.com/contactus) - [Privacy Policy](https://www.mygreatlearning.com/privacy-policy) - [Terms of Use](https://www.mygreatlearning.com/terms) - [Great Learning Careers](https://www.mygreatlearning.com/careers) © 2013 - 2026 Great Learning Education Services Private Limited. All rights reserved [Get our android app](https://play.google.com/store/apps/details?id=com.lms.greatlakes) [Get our ios app](https://apps.apple.com/in/app/great-learning-online-courses/id1016344161) - [Browse Topics](https://www.mygreatlearning.com/blog/) - [AI and Machine Learning](https://www.mygreatlearning.com/blog/artificial-intelligence/) - [Data Science and Analytics](https://www.mygreatlearning.com/blog/data-science/) - [Cloud Computing](https://www.mygreatlearning.com/blog/cloud-computing/) - [Cybersecurity](https://www.mygreatlearning.com/blog/cybersecurity/) - [IT/Software Development](https://www.mygreatlearning.com/blog/software/) - [Career Development](https://www.mygreatlearning.com/blog/career/) - [Digital Marketing](https://www.mygreatlearning.com/blog/digital-marketing/) - [Business Management](https://www.mygreatlearning.com/blog/businessmanagement/) - [UI/UX Design](https://www.mygreatlearning.com/blog/design-thinking/) - [Study Abroad](https://www.mygreatlearning.com/blog/study-abroad/) - [Research and Studies](https://www.mygreatlearning.com/blog/research-and-studies/) - [PG Courses](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/) - [Artificial Intelligence Courses](https://www.mygreatlearning.com/artificial-intelligence/courses) - [Data Science Courses](https://www.mygreatlearning.com/data-science/courses) - [Gen AI Courses](https://www.mygreatlearning.com/gen-ai/courses) - [Management Courses](https://www.mygreatlearning.com/management/courses) - [MBA Courses](https://www.mygreatlearning.com/degrees/mba-courses) - [Cloud Computing Courses](https://www.mygreatlearning.com/cloud-computing/courses) - [Cybersecurity Courses](https://www.mygreatlearning.com/cyber-security/courses) - [Software Engineering Courses](https://www.mygreatlearning.com/software-engineering/courses) - [Design Courses](https://www.mygreatlearning.com/design/courses) - [Master Degree Courses](https://www.mygreatlearning.com/degrees/masters-courses) - [Post Graduate Courses](https://www.mygreatlearning.com/pg-courses) - [Professional Courses](https://www.mygreatlearning.com/professional-certificates) - [Master Courses](https://www.mygreatlearning.com/degrees/masters-courses) - [Free Courses](https://www.mygreatlearning.com/academy) - [Artificial Intelligence Free Courses](https://www.mygreatlearning.com/ai/free-courses) - [Machine Learning Free Courses](https://www.mygreatlearning.com/machine-learning/free-courses) - [Data Science Free Courses](https://www.mygreatlearning.com/data-science/free-courses) - [Cyber Security Free Courses](https://www.mygreatlearning.com/cybersecurity/free-courses) - [Cloud Computing Free Courses](https://www.mygreatlearning.com/cloud-computing/free-courses) - [IT & Software Free Courses](https://www.mygreatlearning.com/software/free-courses) - [Interview Preparation Free Courses](https://www.mygreatlearning.com/interview-preparation/free-courses) - [Digital Marketing Free Courses](https://www.mygreatlearning.com/digital-marketing/free-courses) - [Management Free Courses](https://www.mygreatlearning.com/management/free-courses) - [Resources & Tools]() - [Free Resume Builder](https://www.mygreatlearning.com/academy/pro/resume-builder) - [GenAI Academy](https://www.greatlearning.ai/) - [Tools and Compilers](https://www.mygreatlearning.com/blog/tools-and-compilers/) - [Coding Practice](https://www.mygreatlearning.com/blog/exercises/) - [Quizzes](https://www.mygreatlearning.com/blog/quizzes/) - [Career Options](https://www.mygreatlearning.com/blog/careers-and-roadmap/) - [Project Ideas](https://www.mygreatlearning.com/blog/project-ideas/) - [Interview Questions](https://www.mygreatlearning.com/blog/interview-questions/) - [Follow us on Google](https://www.google.com/preferences/source?q=mygreatlearning.com) - [Browse Topics](https://www.mygreatlearning.com/blog/) - [AI and Machine Learning](https://www.mygreatlearning.com/blog/artificial-intelligence/) - [Data Science and Analytics](https://www.mygreatlearning.com/blog/data-science/) - [Cloud Computing](https://www.mygreatlearning.com/blog/cloud-computing/) - [Cybersecurity](https://www.mygreatlearning.com/blog/cybersecurity/) - [IT/Software Development](https://www.mygreatlearning.com/blog/software/) - [Career Development](https://www.mygreatlearning.com/blog/career/) - [Digital Marketing](https://www.mygreatlearning.com/blog/digital-marketing/) - [Business Management](https://www.mygreatlearning.com/blog/businessmanagement/) - [UI/UX Design](https://www.mygreatlearning.com/blog/design-thinking/) - [Study Abroad](https://www.mygreatlearning.com/blog/study-abroad/) - [Research and Studies](https://www.mygreatlearning.com/blog/research-and-studies/) - [PG Courses](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/) - [Artificial Intelligence Courses](https://www.mygreatlearning.com/artificial-intelligence/courses) - [Data Science Courses](https://www.mygreatlearning.com/data-science/courses) - [Gen AI Courses](https://www.mygreatlearning.com/gen-ai/courses) - [Management Courses](https://www.mygreatlearning.com/management/courses) - [MBA Courses](https://www.mygreatlearning.com/degrees/mba-courses) - [Cloud Computing Courses](https://www.mygreatlearning.com/cloud-computing/courses) - [Cybersecurity Courses](https://www.mygreatlearning.com/cyber-security/courses) - [Software Engineering Courses](https://www.mygreatlearning.com/software-engineering/courses) - [Design Courses](https://www.mygreatlearning.com/design/courses) - [Master Degree Courses](https://www.mygreatlearning.com/degrees/masters-courses) - [Post Graduate Courses](https://www.mygreatlearning.com/pg-courses) - [Professional Courses](https://www.mygreatlearning.com/professional-certificates) - [Master Courses](https://www.mygreatlearning.com/degrees/masters-courses) - [Free Courses](https://www.mygreatlearning.com/academy) - [Artificial Intelligence Free Courses](https://www.mygreatlearning.com/ai/free-courses) - [Machine Learning Free Courses](https://www.mygreatlearning.com/machine-learning/free-courses) - [Data Science Free Courses](https://www.mygreatlearning.com/data-science/free-courses) - [Cyber Security Free Courses](https://www.mygreatlearning.com/cybersecurity/free-courses) - [Cloud Computing Free Courses](https://www.mygreatlearning.com/cloud-computing/free-courses) - [IT & Software Free Courses](https://www.mygreatlearning.com/software/free-courses) - [Interview Preparation Free Courses](https://www.mygreatlearning.com/interview-preparation/free-courses) - [Digital Marketing Free Courses](https://www.mygreatlearning.com/digital-marketing/free-courses) - [Management Free Courses](https://www.mygreatlearning.com/management/free-courses) - [Resources & Tools]() - [Free Resume Builder](https://www.mygreatlearning.com/academy/pro/resume-builder) - [GenAI Academy](https://www.greatlearning.ai/) - [Tools and Compilers](https://www.mygreatlearning.com/blog/tools-and-compilers/) - [Coding Practice](https://www.mygreatlearning.com/blog/exercises/) - [Quizzes](https://www.mygreatlearning.com/blog/quizzes/) - [Career Options](https://www.mygreatlearning.com/blog/careers-and-roadmap/) - [Project Ideas](https://www.mygreatlearning.com/blog/project-ideas/) - [Interview Questions](https://www.mygreatlearning.com/blog/interview-questions/) - [Follow us on Google](https://www.google.com/preferences/source?q=mygreatlearning.com) × Get Free Access to Exclusive Content — Courses, Job Tips, Quizzes, Guides & More Delivered Straight to Your Inbox\! × ## Go Beyond Learning. Get Job-Ready. Build real, in-demand skills for today's jobs by joining our free expert-led courses with hands-on projects and practical AI tools. ![](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%200%200'%3E%3C/svg%3E) ![](https://dtmvamahs40ux.cloudfront.net/gl-academy/course/course-965-intro%20to%20AI.jpg) ### Introduction to Artificial Intelligence 👥 195.2K+ learners ⏱ 4 hrs of learning [Enroll for Free](https://www.mygreatlearning.com/academy/learn-for-free/courses/introduction-to-artificial-intelligence?utm_source=blog&utm_medium=exit_intent_popup&utm_campaign=free_courses&utm_content=introduction_to_ai) ![](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%200%200'%3E%3C/svg%3E) ![](https://dtmvamahs40ux.cloudfront.net/public/course/course-3219-PFB.jpg) ### Python Fundamentals for Beginners 👥 864.8K+ learners ⏱ 4 hrs of learning [Enroll for Free](https://www.mygreatlearning.com/academy/learn-for-free/courses/python-fundamentals-for-beginners?utm_source=blog&utm_medium=exit_intent_popup&utm_campaign=free_courses&utm_content=python_fundamentals) ![](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%200%200'%3E%3C/svg%3E) ![](https://dtmvamahs40ux.cloudfront.net/public/course/course-3327-UI-ux.jpeg) ### UI / UX for Beginners 👥 431.9K+ learners ⏱ 1 hr of learning [Enroll for Free](https://www.mygreatlearning.com/academy/learn-for-free/courses/ui-ux?utm_source=blog&utm_medium=exit_intent_popup&utm_campaign=free_courses&utm_content=ui_ux) [Explore All Courses](https://www.mygreatlearning.com/academy?utm_source=blog&utm_medium=exit_intent_popup&utm_campaign=free_courses&utm_content=explore_all) × ### Go Beyond Learning. Get Job-Ready. Build in-demand skills for today's jobs with free expert-led courses and practical AI tools. [Explore All Courses](https://www.mygreatlearning.com/academy?utm_source=blog&utm_medium=exit_intent_popup&utm_campaign=free_courses&utm_content=mobile_cta) Scroll to Top
Readable Markdown
Table of contents - - - [What is a Priority Queue in C++?](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#what-is-a-priority-queue-in-c) - [C++ Priority Queue Methods](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#c-priority-queue-methods) - [Conclusion](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#conclusion) ### **Contents** 1. [What is Priority Queue in C++?](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#what-is-a-priority-queue-in-C++) 2. [Difference between Priority Queue and Queue](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#difference-between-a-queue-and-priority-queue) 3. [Syntax of Priority Queue](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#syntax) 4. [Syntax to create min-heap for Priority Queue](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#min-heap) 5. [How does c++ Priority Queue work?](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#how-does-c++-priority-queue-work) - [Inserting elements](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#inserting-elements) - [Accessing ele](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#accessing%20elements)[m](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#accessing-elements)[ents](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#accessing%20elements) - [Deleting elements](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#deleting-elements) 6. [C++ Priority Queue Methods](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#C++-priority-queue-methods) 7. [Simple programs of Priority Queue](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#simple-programs-of-priority-queue) 8. [Conclusion](https://www.mygreatlearning.com/blog/priority-queue-in-cpp/#conclusion) Summarize this article with ChatGPT Get key takeaways & ask questions ## **What is a Priority Queue in C++?** A priority queue in c++ is a type of container adapter, which processes only the highest priority element, i.e. the first element will be the maximum of all elements in the queue, and elements are in decreasing order. [C++ Programming Course](https://www.mygreatlearning.com/academy/premium/learn-c-programming-for-beginners-to-advanced) Master key C++ programming concepts like variables, functions, OOP, and control structures. Build real-world projects such as a banking system and grade management tool. Beginner to Advanced Level 8\.1 hrs [Start Free Trial](https://www.mygreatlearning.com/academy/premium/learn-c-programming-for-beginners-to-advanced) ### **Difference between a queue and a priority queue:** | | | |---|---| | Priority Queue | Queue | | Priority Queue container processes the element with the highest priority, whereas no priority exists in a queue. | The queue follows First-in-First-out (FIFO) rule, but in the priority queue highest priority element will be deleted first. | - If more than one element exists with the same priority, then, in this case, the order of queue will be taken. ### **Syntax of Priority Queue:** ```   priority_queue&lt;int> variableName;    ``` **Note**: By default, C++ creates a max-heap for the priority queue. ### **Syntax to create min-heap for the Priority Queue:** ```   priority_queue &lt;int, vector&lt;int>, greater&lt;int>> q;  ``` Where vector\<int\> is a STL container and greater\<int\> is comparator class. ### **How does c++ Priority Queue work?** Priority Queue considers only the highest priority element. ### Inserting elements in a Priority Queue: ``` /* Program to insert elements in a queue*/ #include&lt;iostream> #include&lt;queue> //Header-file for queue using namespace std; int main() { priority_queue&lt;int> p1; p1.push(35); // inserting element in a queue p1.push(40); p1.push(95); while (!p1.empty()) { cout &lt;&lt; ' ' &lt;&lt; p1.top(); //printing elements of queue p1.pop(); } } ``` ### Accessing elements in a Priority Queue: ``` /* Program to access an element of highest priority */ #include&lt;iostream> #include&lt;queue> //Header-file for queue using namespace std; int main() { priority_queue&lt;int> p1; p1.push(35); p1.push(40); p1.push(95); p1.push(25); cout&lt;&lt;p1.top(); //fetch element of highest priority(maximum element) i.e 95 } ``` ### Deleting elements in a Priority Queue: ``` /* Program to delete elements in a queue*/ #include&lt;iostream> #include&lt;queue> //Header-file for queue using namespace std; int main() { priority_queue&lt;int> p1; p1.push(35); p1.push(40); p1.push(95); p1.push(20); // queue : 95 40 35 20 p1.pop(); // queue : 40 35 20 p1.pop(); // queue : 35 20 while (!p1.empty()) { cout &lt;&lt; ' ' &lt;&lt; p1.top(); p1.pop(); } } ``` ## **C++ Priority Queue Methods** Following are the C++ Priority Queue Methods : | | | |---|---| | Methods | Description | | empty() | This method checks whether the priority\_queue container is empty or not. If it is empty, return true, else false. It does not take any parameters. syntax : p1.empty() // p1 is priority\_queue object | | size() | This method gives the number of elements in the priority queue container. It returns the size in an integer. It does not take any parameter. syntax : p2.size() *// p2 is priority\_queue object* | | push() | This method inserts the element into the queue. Firstly, the element is added to the end of the queue, and simultaneously elements reorder themselves with priority. It takes value in the parameter. syntax : p3.push(value) *//value to be inserted* | | pop() | This method delete the top element (highest priority) from the priority\_queue. It does not take any parameter. syntax : p3.pop() // p3 is priority\_queue object | | top() | This method gives the top element from the priority queue container. It does not take any parameter. syntax : p3.top() | | swap() | This method swaps the elements of a priority\_queue with another priority\_queue of the same size and type. It takes the priority queue in a parameter whose values need to be swapped. syntax : p3.swap(p1) | | emplace() | This method adds a new element in a container at the top of the priority queue. It takes value in a parameter. syntax : p3.emplace(value) | Lets see the above methods with a simple program : ### A **Program to show size and empty method:** ``` #include&lt;iostream> #include&lt;queue> //Header-file for queue using namespace std; int main() { priority_queue&lt;int> q1; q1.push(25); //add 25 in a queue q1.push(15); q1.push(30); q1.push(76); q1.push(85); // queue : 85 76 30 25 15 cout&lt;&lt;" Number of elements in a priority queue : "&lt;&lt;q1.size()&lt;&lt;endl; cout&lt;&lt;" The top element is : "&lt;&lt;q1.top()&lt;&lt;endl; //return the maximum element from the queue; q1.pop(); //remove the highest priority element(MAX element) from the queue; // queue : 76 30 25 15 q1.push(55); q1.push(89); q1.push(35); cout&lt;&lt;" After Inserting : "&lt;&lt;endl; //queue : 89 76 55 35 30 25 15 while(!q1.empty()) { cout&lt;&lt;" "&lt;&lt;q1.top()&lt;&lt;" "; q1.pop(); } } ``` ### **Program of swap method :** ``` #include &lt;iostream> #include &lt;queue> using namespace std; int main() { priority_queue&lt;int> p1,q1; //pushing value in p1. p1.push(28); p1.push(69); p1.push(36); p1.push(23); p1.push(14); //pushing value in q1. q1.push(10); q1.push(80); q1.push(32); q1.push(20); q1.push(15); p1.swap(q1); cout&lt;&lt; "elements in p1 : "; while(!p1.empty()) { cout&lt;&lt;" "&lt;&lt;p1.top() &lt;&lt;' '; p1.pop(); } cout&lt;&lt; '\n'; cout&lt;&lt; "elements in m1 : "; while(!q1.empty()) { cout&lt;&lt;" "&lt;&lt;q1.top() &lt;&lt; ' '; q1.pop(); } return 0; } ``` ### A **Program of embrace method :** ``` #include&lt;iostream> #include&lt;queue> //Header-file for queue #include&lt;string> using namespace std; int main() { priority_queue&lt;string> q1; q1.emplace("Ankit"); q1.emplace("Ved"); q1.emplace("Nikita"); q1.emplace("Shaurya"); q1.emplace("Anokhi"); // queue: Ved Shaurya Nikita Anokhi Ankit t while (!q1.empty()) { cout&lt;&lt;" "&lt;&lt;q1.top() &lt;&lt; " "; q1.pop(); } return 0; } ``` ### **C++ Program to implement min-heap :** ``` #include&lt;iostream> #include&lt;queue> //Header-file for queue using namespace std; int main() { // creates a min heap priority_queue &lt;int, vector&lt;int>, greater&lt;int> > p1; p1.push(55); p1.push(1); p1.push(76); p1.push(39); p1.push(23); p1.push(49); p1.push(32); while (!p1.empty()) { cout &lt;&lt;" "&lt;&lt; p1.top() &lt;&lt; " "; p1.pop(); } return 0; } ``` ## **Conclusion** Therefore, Priority Queue is a container that stores elements with priority. Unlike queues, which insert or delete the element based on the FIFO rule, in Priority Queue, elements are removed based on priority. The element with the highest priority is the first one to be removed from the queue. Priority queue supports three operations: **is\_empty** to check if there is no element in the queue,**insert\_with\_priority**, to add an element with priority, and **Pull\_highest\_priority\_element**, to fetch element of highest priority in the queue and show it. The importance of the priority queue is to process the elements based on priority. This brings us to the end of the blog on the concept of Priority Queue in C++. We hope that you found this comprehensive and helpful and were able to gain the required knowledge. If you wish to up-skill and learn more such concepts, you can [check out the pool of Free online courses on Great Learning Academy.](https://www.mygreatlearning.com/academy) Also, if you are preparing for Interviews, check out these [Interview Questions for C++](https://www.mygreatlearning.com/blog/cpp-interview-questions/) to ace it like a pro.
Shard34 (laksa)
Root Hash3011582960318031034
Unparsed URLcom,mygreatlearning!www,/blog/priority-queue-in-cpp/ s443