ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.programiz.com/cpp-programming/priority-queue |
| Last Crawled | 2026-04-16 12:36:27 (1 day ago) |
| First Indexed | 2022-01-05 10:56:08 (4 years ago) |
| HTTP Status Code | 200 |
| Meta Title | C++ Priority Queue |
| Meta Description | In C++, the STL priority_queue provides the functionality of a priority queue data structure. In this tutorial, you will learn about the STL priority_queue with the help of examples. |
| Meta Canonical | null |
| Boilerpipe Text | In C++, the STL
priority_queue
provides the functionality of a priority queue data structure.
A priority queue is a special type of queue in which each element is associated with a priority value and elements are served based on their priority.
Priority Queue Data Structure
To learn more about priority queues, visit our
Priority Queue Data Structure
.
Create a Priority Queue
In order to create a priority queue in C++, we first need to include the
queue
header file.
#include <queue>
Once we import this file, we can create a
priority_queue
using the following syntax:
priority_queue<type> pq;
Here,
type
indicates the data type we want to store in the priority queue. For example,
// create a priority queue of integer type
priority_queue<int> pq_integer;
// create a priority queue of string type
priority_queue<string> pq_string;
Note:
By default, STL
priority_queue
gives the largest element the highest priority.
C++ Priority Queue Methods
In C++, the
priority_queue
class provides various methods to perform different operations on a queue.
Methods
Description
push()
inserts the element into the priority queue
pop()
removes the element with the highest priority
top()
returns the element with the highest priority
size()
returns the number of elements
empty()
returns
true
if the priority_queue is empty
Insert Element to a Priority Queue
We use the
push()
method to insert an element into the priority queue. For example,
#include<iostream>
#include <queue>
using namespace std;
int main() {
// create a queue of int
priority_queue<int> numbers;
// add items to priority_queue
numbers.push(1);
numbers.push(20);
numbers.push(7);
cout << "Priority Queue: ";
// display all elements of numbers
while(!numbers.empty()) {
cout << numbers.top() << ", ";
numbers.pop();
}
cout << endl;
return 0;
}
Output
Priority Queue: 20, 7, 1,
In the above example, we have created a
priority_queue
of integers called
numbers
. Here, we have used the
push()
method to insert the following elements into the queue:
1
,
20
,
7
.
numbers.push(1);
Notice that we have pushed elements in random order but when printing them we get the integers sorted in descending order:
20
,
7
,
1
.
The top of the queue is the maximum element in the queue since
priority_queue
is implemented as
max-heap
by default.
Printing Queue Elements
We cannot iterate through a priority queue like we can with vectors and other containers.
This is why we have used a
while
loop and various
priority_queue
methods to print its elements in the program above.
while(!numbers.empty()) {
cout << numbers.top() << ", ";
numbers.pop();
}
This is because
priority_queue
is an
STL Container Adapter
that provides restrictive access to make it behave like a standard priority queue data structure.
So, we print its
top
and then
pop
the element repeatedly inside a loop until the queue is empty.
We will see about
pop()
,
top()
and
empty()
in the coming sections.
Remove element from the Priority Queue
We can remove an element from the
priority_queue
using the
pop()
method. This removes the element with the highest priority.
#include<iostream>
#include <queue>
using namespace std;
// function prototype for display_priority_queue()
void display_priority_queue(priority_queue<int> pq);
int main() {
// create a queue of int
priority_queue<int> numbers;
// add items to priority_queue
numbers.push(1);
numbers.push(20);
numbers.push(7);
cout << "Initial Priority Queue: ";
display_priority_queue(numbers);
// remove element from queue
numbers.pop();
cout << "Final Priority Queue: ";
display_priority_queue(numbers);
return 0;
}
// utility function to dislay priority queue
void display_priority_queue(priority_queue<int> pq) {
while(!pq.empty()) {
cout << pq.top() << ", ";
pq.pop();
}
cout << endl;
}
Output
Initial Priority Queue: 20, 7, 1,
Final Priority Queue: 7, 1,
In the above example, we have created a
priority_queue
of integers called
numbers
. Initially, the content of the priority queue is
{20, 7, 1}
.
We have then used the
pop()
method to remove the top element:
// pop the top element
numbers.pop();
This removes the element with the highest priority:
20
.
Hence, the final queue contains the elements
7
and
1
. We print the final queue using the
display_priority_queue()
function.
Access Element from the Priority Queue
We access the top element of the
priority_queue
using the
top()
method.
#include<iostream>
#include <queue>
using namespace std;
int main() {
// create a priority queue of int
priority_queue<int> numbers;
// add items to priority_queue
numbers.push(1);
numbers.push(20);
numbers.push(7);
// get the element at the top
int top = numbers.top();
cout << "Top element: " << top;
return 0;
}
Output
Top element: 20
In the above example, we have inserted elements into
priority_queue
in the following order:
1
,
20
,
7
.
Then, we print the top element using:
// returns 20
numbers.top();
Here,
20
has the highest priority, so it is at the top.
Get the size of the Priority Queue
We use the
size()
method to get the number of elements in the
priority_queue
.
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a priority queue of string
priority_queue<string> languages;
// add items to priority_queue
languages.push("C++");
languages.push("Python");
languages.push("Java");
// get the size of queue
int size = languages.size();
cout << "Size of the queue: " << size;
return 0;
}
Output
Size of the queue: 3
In the above example, we have created a
priority_queue
of strings called
languages
and added 3 elements to it.
Then we used the
size()
method to find the number of elements in the queue:
int size = languages.size();
Since we have added 3 elements to the queue,
languages.size()
returns
3
.
Check if the Priority Queue is Empty
We use the
empty()
method to check if the
priority_queue
is empty. This method returns:
1 (true)
- if the priority queue is empty
0 (false)
- if the priority queue is not empty
Example: C++ empty() Method
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a priority queue of ints
priority_queue<string> languages;
cout << "Is the queue empty? ";
// check if the queue is empty
if (languages.empty()) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
cout << "Pushing elements..." << endl;
// push element into the queue
languages.push("Python");
languages.push("C++");
languages.push("Java");
cout << "Is the queue empty? ";
// check if the queue is empty
if (languages.empty()) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Output
Is the queue empty? No
Popping all the elements
Is the queue empty? Yes
In the above example, we have used the
empty()
method to determine if the
languages
priority queue is empty,
if (languages.empty()) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
Initially, the queue has no elements in it. So
languages.empty()
returns
true
.
We then inserted elements into the queue and used the
empty()
method again. This time, it returns
false
.
Min-Heap Priority Queue
We can also create a
min-heap
priority_queue
that arranges elements in ascending order. Its syntax is:
priority_queue<type, vector<type>, greater<type>> pq;
For example,
// integers are arranged in ascending order
priority_queue<int, vector<int>, greater<int>> pq_integers;
In this type of priority queue, the smallest element gets the highest priority.
Example: Min-Heap Priority Queue
#include<iostream>
#include <queue>
using namespace std;
int main() {
// create a priority queue of int
// arranges elements in ascending order
priority_queue<int, vector<int>, greater<int>> numbers;
// add items to priority_queue
numbers.push(1);
numbers.push(20);
numbers.push(7);
// print element with highest priority
cout << "Top element: " << numbers.top();
return 0;
}
Output
Top element: 1 |
| Markdown | 
[Stop copy pasting code you don't actually understand Build the coding confidence you need to become a developer companies will fight for Stop copy pasting code you don't actually understand Ends in Start FREE Trial Start FREE Trial Start FREE Trial Start FREE Trial](https://programiz.pro/?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=programiz&utm_content=default_banner&utm_term=sticky_banner_launch "Start FREE Trial")
[Stop copy pasting code you don't actually understand Build the coding confidence you need to become a developer companies will fight for Stop copy pasting code you don't actually understand Ends in Start FREE Trial Start FREE Trial Start FREE Trial Start FREE Trial](https://programiz.pro/?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=programiz&utm_content=default_banner&utm_term=sticky_banner_launch "Start FREE Trial")
[ ](https://www.programiz.com/ "Programiz")
[Tutorials](https://www.programiz.com/cpp-programming/priority-queue "Programming Tutorials")
[Examples]("Programming Examples")
[ Courses](https://www.programiz.com/cpp-programming/priority-queue "Learn to Code Interactively")
[Try Programiz PRO](https://programiz.pro/learn/master-cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_floating_button "Try Programiz PRO")
Course Index
Tutorials
Courses
[Python]("Python") [JavaScript]("JavaScript") [TypeScript]("TypeScript") [SQL]("SQL") [HTML]("HTML") [CSS]("CSS") [C]("C") [C++]("C++") [Java]("Java") [R]("R") [Ruby]("Ruby") [RUST]("RUST") [Golang]("Golang") [Kotlin]("Kotlin") [Swift]("Swift") [C\#]("C#") [DSA]("DSA")
Become a certified C++
programmer.
[ENROLL](https://programiz.pro/learn/master-cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner)
#### Popular Tutorials
[C++ if...else Statement](https://www.programiz.com/cpp-programming/if-else?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ if...else Statement")
[C++ for Loop](https://www.programiz.com/cpp-programming/for-loop?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ for Loop")
[Arrays in C++](https://www.programiz.com/cpp-programming/arrays?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Arrays in C++")
[Strings in C++](https://www.programiz.com/cpp-programming/strings?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Strings in C++")
[C++ Class & Objects](https://www.programiz.com/cpp-programming/object-class?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ Class & Objects")
[Start Learning C++](https://www.programiz.com/cpp-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ Tutorials")
#### Popular Examples
[Create a simple calculator](https://www.programiz.com/cpp-programming/examples/calculator-switch-case?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Create a simple calculator")
[Check prime number](https://www.programiz.com/cpp-programming/examples/prime-number?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Check prime number")
[Print the Fibonacci sequence](https://www.programiz.com/cpp-programming/examples/fibonacci-series?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Print the Fibonacci sequence")
[Check if a number is palindrome or not](https://www.programiz.com/cpp-programming/examples/palindrome-number?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Check if a number is palindrome or not")
[Program to multiply matrix](https://www.programiz.com/cpp-programming/examples/matrix-multiplication?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Program to multiply matrix")
[Explore C++ Examples](https://www.programiz.com/cpp-programming/examples?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ Examples")
#### Reference Materials
[iostream](https://www.programiz.com/cpp-programming/library-function/iostream?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "iostream")
[cmath](https://www.programiz.com/cpp-programming/library-function/cmath?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "cmath")
[cstring](https://www.programiz.com/cpp-programming/library-function/cstring?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "cstring")
[ctime](https://www.programiz.com/cpp-programming/library-function/ctime?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "ctime")
[View all](https://www.programiz.com/cpp-programming/library-function?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ References")

Created with over a decade of experience.
- [Learn]("Learn")
- [Practice]("Practice")
- [Compete]("Compete")
[Learn Python](https://programiz.pro/learn/master-python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_programiz-pro_courses "Learn Python")
[Learn HTML](https://programiz.pro/course/learn-html?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_html&utm_term=nav_programiz-pro_courses "Learn HTML")
[Learn JavaScript](https://programiz.pro/learn/master-javascript?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_javascript&utm_term=nav_programiz-pro_courses "Learn JavaScript")
[Learn SQL](https://programiz.pro/course/learn-sql-basics?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_sql&utm_term=nav_programiz-pro_courses "Learn SQL")
[Learn DSA](https://programiz.pro/course/dsa-with-python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_dsa&utm_term=nav_programiz-pro_courses "Learn DSA")
[Learn C](https://programiz.pro/learn/master-c-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_c&utm_term=nav_programiz-pro_courses "Learn C")
[Learn C++](https://programiz.pro/learn/master-cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_courses "Learn C++")
[Learn Java](https://programiz.pro/learn/master-java?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_courses "Learn Java")
[View all Courses on ](https://programiz.pro/courses?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "View all Courses on PRO")
[Python Basics](https://programiz.pro/course/practice-python-basics?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_python&utm_term=nav_programiz-pro_practice_courses "Python Basics")
[Python Intermediate](https://programiz.pro/course/practice-python-intermediate?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_python&utm_term=nav_programiz-pro_practice_courses "Python Intermediate")
[C++ Basics](https://programiz.pro/course/practice-cpp-basics?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_practice_courses "C++ Basics")
[C++ Intermediate](https://programiz.pro/course/practice-cpp-intermediate?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_practice_courses "C++ Intermediate")
[C++ OOP](https://programiz.pro/course/practice-cpp-oop?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_practice_courses "C++ OOP")
[C Programming](https://programiz.pro/course/practice-c-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_c&utm_term=nav_programiz-pro_practice_courses "C Programming")
[Java Basics](https://programiz.pro/course/practice-java-basics?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_practice_courses "Java Basics")
[Java Intermediate](https://programiz.pro/course/practice-java-intermediate?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_practice_courses "Java Intermediate")
[Java OOP](https://programiz.pro/course/practice-java-oop?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_practice_courses "Java OOP")
[View all Courses on ](https://programiz.pro/courses?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "View all Courses on PRO")
[Python Challenges](https://programiz.pro/community-challenges/python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_python&utm_term=nav_programiz-pro_challenges "Python Challenges")
[JavaScript Challenges](https://programiz.pro/community-challenges/javascript?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_javascript&utm_term=nav_programiz-pro_challenges "JavaScript Challenges")
[Java Challenges](https://programiz.pro/community-challenges/java?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_challenges "Java Challenges")
[C++ Challenges](https://programiz.pro/community-challenges/cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_challenges "C++ Challenges")
[C Challenges](https://programiz.pro/community-challenges/c?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_c&utm_term=nav_programiz-pro_challenges "C Challenges")
[View all Challenges on ](https://programiz.pro/community-challenges?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "View all Challenges on PRO")
[Learn]()
[Practice]()
[Compete]()
#### Certification Courses
Created with over a decade of experience and thousands of feedback.
[Learn Python](https://programiz.pro/learn/master-python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_python&utm_term=nav_programiz-pro_challenges "Learn Python")
[Learn HTML](https://programiz.pro/course/learn-html?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_html&utm_term=nav_programiz-pro_challenges "Learn HTML")
[Learn JavaScript](https://programiz.pro/learn/master-javascript?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_javascript&utm_term=nav_programiz-pro_challenges "Learn JavaScript")
[Learn SQL](https://programiz.pro/course/learn-sql-basics?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_sql&utm_term=nav_programiz-pro_challenges "Learn SQL")
[Learn DSA](https://programiz.pro/course/dsa-with-python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_dsa&utm_term=nav_programiz-pro_challenges "Learn DSA")
[View all Courses on ](https://programiz.pro/courses?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "View all Courses on PRO")
[Learn C](https://programiz.pro/learn/master-c-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_c&utm_term=nav_programiz-pro_challenges "Learn C")
[Learn C++](https://programiz.pro/learn/master-cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_challenges "Learn C++")
[Learn Java](https://programiz.pro/learn/master-java?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_challenges "Learn Java")
[Python]("Python")
[JavaScript]("JavaScript")
[TypeScript]("TypeScript")
[SQL]("SQL")
[HTML]("HTML")
[CSS]("CSS")
[C]("C")
[C++]("C++")
[Java]("Java")
[More languages]("More languages")
### Become a certified C++ programmer.
[Try Programiz PRO\!](https://programiz.pro/learn/master-cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner)
#### Popular Tutorials
[C++ if...else Statement](https://www.programiz.com/cpp-programming/if-else?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ if...else Statement")
[C++ for Loop](https://www.programiz.com/cpp-programming/for-loop?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ for Loop")
[Arrays in C++](https://www.programiz.com/cpp-programming/arrays?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Arrays in C++")
[Strings in C++](https://www.programiz.com/cpp-programming/strings?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "Strings in C++")
[C++ Class & Objects](https://www.programiz.com/cpp-programming/object-class?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ Class & Objects")
[Start Learning C++](https://www.programiz.com/cpp-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ tutorials")
[All C++ Tutorials](https://www.programiz.com/cpp-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "All C++ tutorials")
#### Reference Materials
[iostream](https://www.programiz.com/cpp-programming/library-function/iostream?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "iostream")
[cmath](https://www.programiz.com/cpp-programming/library-function/cmath?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "cmath")
[cstring](https://www.programiz.com/cpp-programming/library-function/cstring?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "cstring")
[ctime](https://www.programiz.com/cpp-programming/library-function/ctime?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "ctime")
[View all](https://www.programiz.com/cpp-programming/library-function?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_tutorials_banner "C++ References")
[Python]("Python")
[JavaScript]("JavaScript")
[C]("C")
[C++]("C++")
[Java]("Java")
[R]("R")
[Kotlin]("Kotlin")
Become a certified C++
programmer.
[Try Programiz PRO\!](https://programiz.pro/learn/master-cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_examples_banner)
#### Popular Examples
[Create a simple calculator](https://www.programiz.com/cpp-programming/examples/calculator-switch-case?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_examples_banner "Create a simple calculator")
[Check prime number](https://www.programiz.com/cpp-programming/examples/prime-number?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_examples_banner "Check prime number")
[Print the Fibonacci sequence](https://www.programiz.com/cpp-programming/examples/fibonacci-series?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_examples_banner "Print the Fibonacci sequence")
[Check if a number is palindrome or not](https://www.programiz.com/cpp-programming/examples/palindrome-number?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_examples_banner "Check if a number is palindrome or not")
[Program to multiply matrix](https://www.programiz.com/cpp-programming/examples/matrix-multiplication?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_examples_banner "Program to multiply matrix")
[All C++ Examples](https://www.programiz.com/cpp-programming/examples?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_examples_banner "C++ Examples")
- ### Introduction to C++
- [Getting Started With C++](https://www.programiz.com/cpp-programming/getting-started "Getting Started With C++")
- [Your First C++ Program](https://www.programiz.com/cpp-programming/first-program "Your First C++ Program")
- [C++ Comments](https://www.programiz.com/cpp-programming/comments "C++ Comments")
- ### C++ Fundamentals
- [C++ Keywords and Identifiers](https://www.programiz.com/cpp-programming/keywords-identifiers "C++ Keywords and Identifiers")
- [C++ Variables, Literals and Constants](https://www.programiz.com/cpp-programming/variables-literals "C++ Variables, Literals and Constants")
- [C++ Data Types](https://www.programiz.com/cpp-programming/data-types "C++ Data Types")
- [C++ Type Modifiers](https://www.programiz.com/cpp-programming/type-modifiers "C++ Type Modifiers")
- [C++ Constants](https://www.programiz.com/cpp-programming/constants "C++ Constants")
- [C++ Basic Input/Output](https://www.programiz.com/cpp-programming/input-output "C++ Basic Input/Output")
- [C++ Operators](https://www.programiz.com/cpp-programming/operators "C++ Operators")
- ### Flow Control
- [C++ Relational and Logical Operators](https://www.programiz.com/cpp-programming/relational-logical-operators "C++ Relational and Logical Operators")
- [C++ if, if...else and Nested if...else](https://www.programiz.com/cpp-programming/if-else "C++ if, if...else and Nested if...else")
- [C++ for Loop](https://www.programiz.com/cpp-programming/for-loop "C++ for Loop")
- [C++ while and do...while Loop](https://www.programiz.com/cpp-programming/do-while-loop "C++ while and do...while Loop")
- [C++ break Statement](https://www.programiz.com/cpp-programming/break-statement "C++ break Statement")
- [C++ continue Statement](https://www.programiz.com/cpp-programming/continue-statement "C++ continue Statement")
- [C++ goto Statement](https://www.programiz.com/cpp-programming/goto "C++ goto Statement")
- [C++ switch..case Statement](https://www.programiz.com/cpp-programming/switch-case "C++ switch..case Statement")
- [C++ Ternary Operator](https://www.programiz.com/cpp-programming/ternary-operator "C++ Ternary Operator")
- ### Functions
- [C++ Functions](https://www.programiz.com/cpp-programming/function "C++ Functions")
- [C++ Programming Default Arguments](https://www.programiz.com/cpp-programming/default-argument "C++ Programming Default Arguments")
- [C++ Function Overloading](https://www.programiz.com/cpp-programming/function-overloading "C++ Function Overloading")
- [C++ Inline Functions](https://www.programiz.com/cpp-programming/inline-function "C++ Inline Functions")
- [C++ Recursion](https://www.programiz.com/cpp-programming/recursion "C++ Recursion")
- ### Arrays and Strings
- [C++ Arrays](https://www.programiz.com/cpp-programming/arrays "C++ Arrays")
- [C++ Array to Function](https://www.programiz.com/cpp-programming/passing-arrays-function "C++ Array to Function")
- [C++ Multidimensional Arrays](https://www.programiz.com/cpp-programming/multidimensional-arrays "C++ Multidimensional Arrays")
- [C++ String](https://www.programiz.com/cpp-programming/strings "C++ String")
- [C++ String Class](https://www.programiz.com/cpp-programming/string-class "C++ String Class")
- ### Pointers and References
- [C++ Pointers](https://www.programiz.com/cpp-programming/pointers "C++ Pointers")
- [C++ Pointers and Arrays](https://www.programiz.com/cpp-programming/pointers-arrays "C++ Pointers and Arrays")
- [C++ References: Using Pointers](https://www.programiz.com/cpp-programming/references "C++ References: Using Pointers")
- [C++ Call by Reference: Using pointers](https://www.programiz.com/cpp-programming/pointers-function "C++ Call by Reference: Using pointers")
- [C++ Memory Management: new and delete](https://www.programiz.com/cpp-programming/memory-management "C++ Memory Management: new and delete")
- ### Structures and Enumerations
- [C++ Structures](https://www.programiz.com/cpp-programming/structure "C++ Structures")
- [C++ Structure and Function](https://www.programiz.com/cpp-programming/structure-function "C++ Structure and Function")
- [C++ Pointers to Structure](https://www.programiz.com/cpp-programming/structure-pointer "C++ Pointers to Structure")
- [C++ Enumeration](https://www.programiz.com/cpp-programming/enumeration "C++ Enumeration")
- ### Object Oriented Programming
- [C++ Classes and Objects](https://www.programiz.com/cpp-programming/object-class "C++ Classes and Objects")
- [C++ Constructors](https://www.programiz.com/cpp-programming/constructors "C++ Constructors")
- [C++ Constructor Overloading](https://www.programiz.com/cpp-programming/constructor-overloading "C++ Constructor Overloading")
- [C++ Destructors](https://www.programiz.com/cpp-programming/destructors "C++ Destructors")
- [C++ Access Modifiers](https://www.programiz.com/cpp-programming/access-modifiers "C++ Access Modifiers")
- [C++ Encapsulation](https://www.programiz.com/cpp-programming/encapsulation "C++ Encapsulation")
- [C++ friend Function and friend Classes](https://www.programiz.com/cpp-programming/friend-function-class "C++ friend Function and friend Classes")
- ### Inheritance & Polymorphism
- [C++ Inheritance](https://www.programiz.com/cpp-programming/inheritance "C++ Inheritance")
- [C++ Public, Protected and Private Inheritance](https://www.programiz.com/cpp-programming/public-protected-private-inheritance "C++ Public, Protected and Private Inheritance")
- [C++ Multiple, Multilevel and Hierarchical Inheritance](https://www.programiz.com/cpp-programming/multilevel-multiple-inheritance "C++ Multiple, Multilevel and Hierarchical Inheritance")
- [C++ Function Overriding](https://www.programiz.com/cpp-programming/function-overriding "C++ Function Overriding")
- [C++ Virtual Functions](https://www.programiz.com/cpp-programming/virtual-functions "C++ Virtual Functions")
- [C++ Abstract Class and Pure Virtual Function](https://www.programiz.com/cpp-programming/pure-virtual-funtion "C++ Abstract Class and Pure Virtual Function")
- ### STL - Vector, Queue & Stack
- [C++ Standard Template Library](https://www.programiz.com/cpp-programming/standard-template-library "C++ Standard Template Library")
- [C++ STL Containers](https://www.programiz.com/cpp-programming/stl-containers "C++ STL Containers")
- [C++ std::array](https://www.programiz.com/cpp-programming/std-array "C++ std::array")
- [C++ Vectors](https://www.programiz.com/cpp-programming/vectors "C++ Vectors")
- [C++ List](https://www.programiz.com/cpp-programming/list "C++ List")
- [C++ Forward List](https://www.programiz.com/cpp-programming/forward-list "C++ Forward List")
- [C++ Queue](https://www.programiz.com/cpp-programming/queue "C++ Queue")
- [C++ Deque](https://www.programiz.com/cpp-programming/deque "C++ Deque")
- [C++ Priority Queue](https://www.programiz.com/dsa/priority-queue "C++ Priority Queue")
- [C++ Stack](https://www.programiz.com/cpp-programming/stack "C++ Stack")
- ### STL - Map & Set
- [C++ Map](https://www.programiz.com/cpp-programming/map "C++ Map")
- [C++ Set](https://www.programiz.com/cpp-programming/set "C++ Set")
- [C++ Multimap](https://www.programiz.com/cpp-programming/multimap "C++ Multimap")
- [C++ Multiset](https://www.programiz.com/cpp-programming/multiset "C++ Multiset")
- [C++ Unordered Map](https://www.programiz.com/cpp-programming/unordered-map "C++ Unordered Map")
- [C++ Unordered Set](https://www.programiz.com/cpp-programming/unordered-set "C++ Unordered Set")
- [C++ Unordered Multiset](https://www.programiz.com/cpp-programming/unordered-multiset "C++ Unordered Multiset")
- [C++ Unordered Multimap](https://www.programiz.com/cpp-programming/unordered-multimap "C++ Unordered Multimap")
- ### STL - Iterators & Algorithms
- [C++ Iterators](https://www.programiz.com/cpp-programming/iterators "C++ Iterators")
- [C++ Algorithm](https://www.programiz.com/cpp-programming/algorithm "C++ Algorithm")
- [C++ Functor](https://www.programiz.com/cpp-programming/functors "C++ Functor")
- ### Additional Topics
- [C++ Exceptions Handling](https://www.programiz.com/cpp-programming/exception-handling "C++ Exceptions Handling (With Examples)")
- [C++ File Handling](https://www.programiz.com/cpp-programming/file-handling "C++ File Handling")
- [C++ Ranged for Loop](https://www.programiz.com/cpp-programming/ranged-for-loop "C++ Ranged for Loop")
- [C++ Nested Loop](https://www.programiz.com/cpp-programming/nested-loops "C++ Nested Loop")
- [C++ Function Template](https://www.programiz.com/cpp-programming/function-template "C++ Function Template")
- [C++ Class Templates](https://www.programiz.com/cpp-programming/class-templates "C++ Class Templates")
- [C++ Type Conversion](https://www.programiz.com/cpp-programming/type-conversion "C++ Type Conversion")
- [C++ Type Conversion Operators](https://www.programiz.com/cpp-programming/type-conversion-operators "C++ Type Conversion Operators")
- [C++ Operator Overloading](https://www.programiz.com/cpp-programming/operator-overloading "C++ Operator Overloading")
- ### Advanced Topics
- [C++ 11](https://www.programiz.com/cpp-programming/cpp-11 "C++ 11")
- [C++ Lambda](https://www.programiz.com/cpp-programming/lambda-expression "C++ Lambda")
- [C++ Namespaces](https://www.programiz.com/cpp-programming/namespaces "C++ Namespaces")
- [C++ Preprocessors and Macros](https://www.programiz.com/cpp-programming/preprocessor-macros "C++ Preprocessors and Macros")
- [C++ Storage Class](https://www.programiz.com/cpp-programming/storage-class "C++ Storage Class")
- [C++ Bitwise Operators](https://www.programiz.com/cpp-programming/bitwise-operators "C++ Bitwise Operators")
- [C++ Assert](https://www.programiz.com/cpp-programming/assertions "C++ Assert")
- [C++ Buffers](https://www.programiz.com/cpp-programming/buffer "C++ Buffers")
- [C++ istream](https://www.programiz.com/cpp-programming/istream "C++ istream")
- [C++ ostream](https://www.programiz.com/cpp-programming/ostream "C++ ostream")
### C++ Tutorials
- [C++ Queue](https://www.programiz.com/cpp-programming/queue)
- [C++ STL Containers](https://www.programiz.com/cpp-programming/stl-containers)
- [C++ Stack](https://www.programiz.com/cpp-programming/stack)
- [C++ Standard Template Library](https://www.programiz.com/cpp-programming/standard-template-library)
- [C++ Deque](https://www.programiz.com/cpp-programming/deque)
- [C++ Unordered Set](https://www.programiz.com/cpp-programming/unordered-set)
# C++ Priority Queue
In C++, the STL `priority_queue` provides the functionality of a priority queue data structure.
A priority queue is a special type of queue in which each element is associated with a priority value and elements are served based on their priority.

Priority Queue Data Structure
To learn more about priority queues, visit our [Priority Queue Data Structure](https://www.programiz.com/dsa/priority-queue).
***
## Create a Priority Queue
In order to create a priority queue in C++, we first need to include the `queue` header file.
```
#include <queue>
```
Once we import this file, we can create a `priority_queue` using the following syntax:
```
priority_queue<type> pq;
```
Here, `type` indicates the data type we want to store in the priority queue. For example,
```
// create a priority queue of integer type
priority_queue<int> pq_integer;
// create a priority queue of string type
priority_queue<string> pq_string;
```
**Note:** By default, STL `priority_queue` gives the largest element the highest priority.
***
## C++ Priority Queue Methods
In C++, the `priority_queue` class provides various methods to perform different operations on a queue.
| Methods | Description |
|---|---|
| `push()` | inserts the element into the priority queue |
| `pop()` | removes the element with the highest priority |
| `top()` | returns the element with the highest priority |
| `size()` | returns the number of elements |
| `empty()` | returns `true` if the priority\_queue is empty |
***
## Insert Element to a Priority Queue
We use the `push()` method to insert an element into the priority queue. For example,
```
#include<iostream>
#include <queue>
using namespace std;
int main() {
// create a queue of int
priority_queue<int> numbers;
// add items to priority_queue numbers.push(1); numbers.push(20); numbers.push(7);
cout << "Priority Queue: ";
// display all elements of numbers
while(!numbers.empty()) {
cout << numbers.top() << ", ";
numbers.pop();
}
cout << endl;
return 0;
}
```
**Output**
```
Priority Queue: 20, 7, 1,
```
In the above example, we have created a `priority_queue` of integers called numbers. Here, we have used the `push()` method to insert the following elements into the queue: **1**, **20**, **7**.
```
numbers.push(1);
```
Notice that we have pushed elements in random order but when printing them we get the integers sorted in descending order: **20**, **7**, **1**.
The top of the queue is the maximum element in the queue since `priority_queue` is implemented as **max-heap** by default.
***
### Printing Queue Elements
We cannot iterate through a priority queue like we can with vectors and other containers.
This is why we have used a `while` loop and various `priority_queue` methods to print its elements in the program above.
```
while(!numbers.empty()) {
cout << numbers.top() << ", ";
numbers.pop();
}
```
This is because `priority_queue` is an [STL Container Adapter](https://www.programiz.com/cpp-programming/stl-containers#container-adapter) that provides restrictive access to make it behave like a standard priority queue data structure.
So, we print its **top** and then **pop** the element repeatedly inside a loop until the queue is empty.
We will see about `pop()`, `top()` and `empty()` in the coming sections.
***
## Remove element from the Priority Queue
We can remove an element from the `priority_queue` using the `pop()` method. This removes the element with the highest priority.
```
#include<iostream>
#include <queue>
using namespace std;
// function prototype for display_priority_queue()
void display_priority_queue(priority_queue<int> pq);
int main() {
// create a queue of int
priority_queue<int> numbers;
// add items to priority_queue
numbers.push(1);
numbers.push(20);
numbers.push(7);
cout << "Initial Priority Queue: ";
display_priority_queue(numbers);
// remove element from queue numbers.pop();
cout << "Final Priority Queue: ";
display_priority_queue(numbers);
return 0;
}
// utility function to dislay priority queue
void display_priority_queue(priority_queue<int> pq) {
while(!pq.empty()) {
cout << pq.top() << ", ";
pq.pop();
}
cout << endl;
}
```
**Output**
```
Initial Priority Queue: 20, 7, 1, Final Priority Queue: 7, 1,
```
In the above example, we have created a `priority_queue` of integers called numbers. Initially, the content of the priority queue is `{20, 7, 1}`.
We have then used the `pop()` method to remove the top element:
```
// pop the top element
numbers.pop();
```
This removes the element with the highest priority: **20**.
Hence, the final queue contains the elements **7** and **1**. We print the final queue using the `display_priority_queue()` function.
***
## Access Element from the Priority Queue
We access the top element of the `priority_queue` using the `top()` method.
```
#include<iostream>
#include <queue>
using namespace std;
int main() {
// create a priority queue of int
priority_queue<int> numbers;
// add items to priority_queue
numbers.push(1);
numbers.push(20);
numbers.push(7);
// get the element at the top int top = numbers.top();
cout << "Top element: " << top;
return 0;
}
```
**Output**
```
Top element: 20
```
In the above example, we have inserted elements into `priority_queue` in the following order: **1**, **20**, **7**.
Then, we print the top element using:
```
// returns 20
numbers.top();
```
Here, **20** has the highest priority, so it is at the top.
***
## Get the size of the Priority Queue
We use the `size()` method to get the number of elements in the `priority_queue`.
```
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a priority queue of string
priority_queue<string> languages;
// add items to priority_queue
languages.push("C++");
languages.push("Python");
languages.push("Java");
// get the size of queue int size = languages.size();
cout << "Size of the queue: " << size;
return 0;
}
```
**Output**
```
Size of the queue: 3
```
In the above example, we have created a `priority_queue` of strings called languages and added 3 elements to it.
Then we used the `size()` method to find the number of elements in the queue:
```
int size = languages.size();
```
Since we have added 3 elements to the queue, `languages.size()` returns **3**.
***
## Check if the Priority Queue is Empty
We use the `empty()` method to check if the `priority_queue` is empty. This method returns:
- **1 (true)** - if the priority queue is empty
- **0 (false)** - if the priority queue is not empty
***
### Example: C++ empty() Method
```
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a priority queue of ints
priority_queue<string> languages;
cout << "Is the queue empty? ";
// check if the queue is empty if (languages.empty()) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
cout << "Pushing elements..." << endl;
// push element into the queue
languages.push("Python");
languages.push("C++");
languages.push("Java");
cout << "Is the queue empty? ";
// check if the queue is empty if (languages.empty()) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
```
**Output**
```
Is the queue empty? No Popping all the elements Is the queue empty? Yes
```
In the above example, we have used the `empty()` method to determine if the languages priority queue is empty,
```
if (languages.empty()) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
```
Initially, the queue has no elements in it. So `languages.empty()` returns `true`.
We then inserted elements into the queue and used the `empty()` method again. This time, it returns `false`.
***
## Min-Heap Priority Queue
We can also create a **min-heap** `priority_queue` that arranges elements in ascending order. Its syntax is:
```
priority_queue<type, vector<type>, greater<type>> pq;
```
For example,
```
// integers are arranged in ascending order
priority_queue<int, vector<int>, greater<int>> pq_integers;
```
In this type of priority queue, the smallest element gets the highest priority.
***
### Example: Min-Heap Priority Queue
```
#include<iostream>
#include <queue>
using namespace std;
int main() {
// create a priority queue of int // arranges elements in ascending order priority_queue<int, vector<int>, greater<int>> numbers;
// add items to priority_queue
numbers.push(1);
numbers.push(20);
numbers.push(7);
// print element with highest priority
cout << "Top element: " << numbers.top();
return 0;
}
```
**Output**
```
Top element: 1
```
### Table of Contents
- [Create C++ Priority Queue](https://www.programiz.com/cpp-programming/priority-queue#create)
- [Priority Queue Methods](https://www.programiz.com/cpp-programming/priority-queue#priority_queue-methods)
- [Insert Elements](https://www.programiz.com/cpp-programming/priority-queue#push)
- [Remove Elements](https://www.programiz.com/cpp-programming/priority-queue#pop)
- [Access Elements](https://www.programiz.com/cpp-programming/priority-queue#top)
- [Find Size of Priority Queue](https://www.programiz.com/cpp-programming/priority-queue#size-empty)
- [Check if Queue is Empty](https://www.programiz.com/cpp-programming/priority-queue#empty)
- [Min-Heap Priority Queue](https://www.programiz.com/cpp-programming/priority-queue#min-heap)
Share on:
Did you find this article helpful?
Your builder path starts here. Builders don't just know how to code, they create solutions that matter.
Escape tutorial hell and ship real projects.
[Try Programiz PRO](https://programiz.pro/?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_content=interests_learn_coding&utm_term=tutorial_page_footer_banner "Programiz PRO: Premium Learning Platform from Programiz")
- Real-World Projects
- On-Demand Learning
- AI Mentor
- Builder Community
### Related Tutorials
[C++ Tutorial C++ Queue](https://www.programiz.com/cpp-programming/queue)
[C++ Tutorial C++ STL Containers](https://www.programiz.com/cpp-programming/stl-containers)
[C++ Tutorial C++ Stack](https://www.programiz.com/cpp-programming/stack)
[C++ Tutorial C++ Standard Template Library](https://www.programiz.com/cpp-programming/standard-template-library)
#### Free Tutorials
- [Python 3 Tutorials](https://www.programiz.com/python-programming "Python 3 Tutorials")
- [SQL Tutorials](https://www.programiz.com/sql "SQL Tutorials")
- [R Tutorials](https://www.programiz.com/r "R Tutorials")
- [HTML Tutorials](https://www.programiz.com/html "HTML Tutorials")
- [CSS Tutorials](https://www.programiz.com/css "CSS Tutorials")
- [JavaScript Tutorials](https://www.programiz.com/javascript "JavaScript Tutorials")
- [TypeScript Tutorials](https://www.programiz.com/typescript "TypeScript Tutorials")
- [Java Tutorials](https://www.programiz.com/java-programming "Java Tutorials")
- [C Tutorials](https://www.programiz.com/c-programming "C Tutorials")
- [C++ Tutorials](https://www.programiz.com/cpp-programming "C++ Tutorials")
- [DSA Tutorials](https://www.programiz.com/dsa "Data Structures and Algorithms")
- [C\# Tutorials](https://www.programiz.com/csharp-programming "C# Tutorials")
- [Golang Tutorials](https://www.programiz.com/golang "Golang Tutorials")
- [Kotlin Tutorials](https://www.programiz.com/kotlin-programming "Kotlin Tutorials")
- [Swift Tutorials](https://www.programiz.com/swift-programming "Swift Tutorials")
- [Rust Tutorials](https://www.programiz.com/rust "Rust Tutorials")
- [Ruby Tutorials](https://www.programiz.com/ruby "Ruby Tutorials")
#### Paid Courses
- [Master Python](https://programiz.pro/learn/master-python "Master Python")
- [Learn SQL](https://programiz.pro/course/learn-sql-basics "Learn SQL")
- [Learn HTML](https://programiz.pro/course/learn-html "Learn HTML")
- [Master JavaScript](https://programiz.pro/learn/master-javascript "Master JavaScript")
- [Master C](https://programiz.pro/learn/master-c-programming "Master C")
- [Master C++](https://programiz.pro/learn/master-cpp "Master C++")
- [Master Java](https://programiz.pro/learn/master-java "Master Java")
- [Master DSA with Python](https://programiz.pro/learn/master-dsa-with-python "Master DSA with Python")
#### Online Compilers
- [Python Compiler](https://www.programiz.com/python-programming/online-compiler "Python Compiler")
- [R Compiler](https://www.programiz.com/r/online-compiler "R Compiler")
- [SQL Editor](https://www.programiz.com/sql/online-compiler "SQL Editor")
- [HTML/CSS Editor](https://www.programiz.com/html/online-compiler "HTML/CSS Editor")
- [JavaScript Editor](https://www.programiz.com/javascript/online-compiler "JavaScript Editor")
- [TypeScript Editor](https://www.programiz.com/typescript/online-compiler "TypeScript Editor")
- [Java Compiler](https://www.programiz.com/java-programming/online-compiler "Java Compiler")
- [C Compiler](https://www.programiz.com/c-programming/online-compiler "C Compiler")
- [C++ Compiler](https://www.programiz.com/cpp-programming/online-compiler "C++ Compiler")
- [C\# Compiler](https://www.programiz.com/csharp-programming/online-compiler "C# Compiler")
- [Go Compiler](https://www.programiz.com/golang/online-compiler "Go Compiler")
- [PHP Compiler](https://www.programiz.com/php/online-compiler "PHP Compiler")
- [Swift Compiler](https://www.programiz.com/swift/online-compiler "Swift Compiler")
- [Rust Compiler](https://www.programiz.com/rust/online-compiler "Rust Compiler")
- [Ruby Compiler](https://www.programiz.com/ruby/online-compiler "Ruby Compiler")
#### Mobile Apps
- [Learn Python App](https://www.programiz.com/learn-python "Learn Python: Programiz")
- [Learn C App](https://www.programiz.com/learn-c "Learn C Programming: Programiz")
- [Learn Java App](https://www.programiz.com/learn-java "Learn Java: Programiz")
- [Learn C++ App](https://www.programiz.com/learn-cpp "Learn C++: Programiz")
#### Company
- [Change Ad Consent]()
- [Do not sell my data]()
- [About](https://www.programiz.com/about-us "About us")
- [Contact](https://www.programiz.com/contact "Contact us")
- [Blog](https://www.programiz.com/blog "Blog")
- [Youtube](https://www.youtube.com/channel/UCREFp3D_n8JfcDonlm7Mpyw "Programiz on Youtube")
- [Careers](https://www.programiz.com/careers "Careers")
- [Advertising](https://www.programiz.com/advertise "Advertise with us")
- [Privacy Policy](https://www.programiz.com/privacy-policy "Privacy Policy")
- [Terms & Conditions](https://www.programiz.com/terms-of-use "Terms & Conditions")
© Parewa Labs Pvt. Ltd. All rights reserved. |
| Readable Markdown | In C++, the STL `priority_queue` provides the functionality of a priority queue data structure.
A priority queue is a special type of queue in which each element is associated with a priority value and elements are served based on their priority.

Priority Queue Data Structure
To learn more about priority queues, visit our [Priority Queue Data Structure](https://www.programiz.com/dsa/priority-queue).
***
## Create a Priority Queue
In order to create a priority queue in C++, we first need to include the `queue` header file.
```
#include <queue>
```
Once we import this file, we can create a `priority_queue` using the following syntax:
```
priority_queue<type> pq;
```
Here, `type` indicates the data type we want to store in the priority queue. For example,
```
// create a priority queue of integer type
priority_queue<int> pq_integer;
// create a priority queue of string type
priority_queue<string> pq_string;
```
**Note:** By default, STL `priority_queue` gives the largest element the highest priority.
***
## C++ Priority Queue Methods
In C++, the `priority_queue` class provides various methods to perform different operations on a queue.
| Methods | Description |
|---|---|
| `push()` | inserts the element into the priority queue |
| `pop()` | removes the element with the highest priority |
| `top()` | returns the element with the highest priority |
| `size()` | returns the number of elements |
| `empty()` | returns `true` if the priority\_queue is empty |
***
## Insert Element to a Priority Queue
We use the `push()` method to insert an element into the priority queue. For example,
```
#include<iostream>
#include <queue>
using namespace std;
int main() {
// create a queue of int
priority_queue<int> numbers;
// add items to priority_queue numbers.push(1); numbers.push(20); numbers.push(7);
cout << "Priority Queue: ";
// display all elements of numbers
while(!numbers.empty()) {
cout << numbers.top() << ", ";
numbers.pop();
}
cout << endl;
return 0;
}
```
**Output**
```
Priority Queue: 20, 7, 1,
```
In the above example, we have created a `priority_queue` of integers called numbers. Here, we have used the `push()` method to insert the following elements into the queue: **1**, **20**, **7**.
```
numbers.push(1);
```
Notice that we have pushed elements in random order but when printing them we get the integers sorted in descending order: **20**, **7**, **1**.
The top of the queue is the maximum element in the queue since `priority_queue` is implemented as **max-heap** by default.
***
### Printing Queue Elements
We cannot iterate through a priority queue like we can with vectors and other containers.
This is why we have used a `while` loop and various `priority_queue` methods to print its elements in the program above.
```
while(!numbers.empty()) {
cout << numbers.top() << ", ";
numbers.pop();
}
```
This is because `priority_queue` is an [STL Container Adapter](https://www.programiz.com/cpp-programming/stl-containers#container-adapter) that provides restrictive access to make it behave like a standard priority queue data structure.
So, we print its **top** and then **pop** the element repeatedly inside a loop until the queue is empty.
We will see about `pop()`, `top()` and `empty()` in the coming sections.
***
## Remove element from the Priority Queue
We can remove an element from the `priority_queue` using the `pop()` method. This removes the element with the highest priority.
```
#include<iostream>
#include <queue>
using namespace std;
// function prototype for display_priority_queue()
void display_priority_queue(priority_queue<int> pq);
int main() {
// create a queue of int
priority_queue<int> numbers;
// add items to priority_queue
numbers.push(1);
numbers.push(20);
numbers.push(7);
cout << "Initial Priority Queue: ";
display_priority_queue(numbers);
// remove element from queue numbers.pop();
cout << "Final Priority Queue: ";
display_priority_queue(numbers);
return 0;
}
// utility function to dislay priority queue
void display_priority_queue(priority_queue<int> pq) {
while(!pq.empty()) {
cout << pq.top() << ", ";
pq.pop();
}
cout << endl;
}
```
**Output**
```
Initial Priority Queue: 20, 7, 1, Final Priority Queue: 7, 1,
```
In the above example, we have created a `priority_queue` of integers called numbers. Initially, the content of the priority queue is `{20, 7, 1}`.
We have then used the `pop()` method to remove the top element:
```
// pop the top element
numbers.pop();
```
This removes the element with the highest priority: **20**.
Hence, the final queue contains the elements **7** and **1**. We print the final queue using the `display_priority_queue()` function.
***
## Access Element from the Priority Queue
We access the top element of the `priority_queue` using the `top()` method.
```
#include<iostream>
#include <queue>
using namespace std;
int main() {
// create a priority queue of int
priority_queue<int> numbers;
// add items to priority_queue
numbers.push(1);
numbers.push(20);
numbers.push(7);
// get the element at the top int top = numbers.top();
cout << "Top element: " << top;
return 0;
}
```
**Output**
```
Top element: 20
```
In the above example, we have inserted elements into `priority_queue` in the following order: **1**, **20**, **7**.
Then, we print the top element using:
```
// returns 20
numbers.top();
```
Here, **20** has the highest priority, so it is at the top.
***
## Get the size of the Priority Queue
We use the `size()` method to get the number of elements in the `priority_queue`.
```
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a priority queue of string
priority_queue<string> languages;
// add items to priority_queue
languages.push("C++");
languages.push("Python");
languages.push("Java");
// get the size of queue int size = languages.size();
cout << "Size of the queue: " << size;
return 0;
}
```
**Output**
```
Size of the queue: 3
```
In the above example, we have created a `priority_queue` of strings called languages and added 3 elements to it.
Then we used the `size()` method to find the number of elements in the queue:
```
int size = languages.size();
```
Since we have added 3 elements to the queue, `languages.size()` returns **3**.
***
## Check if the Priority Queue is Empty
We use the `empty()` method to check if the `priority_queue` is empty. This method returns:
- **1 (true)** - if the priority queue is empty
- **0 (false)** - if the priority queue is not empty
***
### Example: C++ empty() Method
```
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a priority queue of ints
priority_queue<string> languages;
cout << "Is the queue empty? ";
// check if the queue is empty if (languages.empty()) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
cout << "Pushing elements..." << endl;
// push element into the queue
languages.push("Python");
languages.push("C++");
languages.push("Java");
cout << "Is the queue empty? ";
// check if the queue is empty if (languages.empty()) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
```
**Output**
```
Is the queue empty? No Popping all the elements Is the queue empty? Yes
```
In the above example, we have used the `empty()` method to determine if the languages priority queue is empty,
```
if (languages.empty()) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
```
Initially, the queue has no elements in it. So `languages.empty()` returns `true`.
We then inserted elements into the queue and used the `empty()` method again. This time, it returns `false`.
***
## Min-Heap Priority Queue
We can also create a **min-heap** `priority_queue` that arranges elements in ascending order. Its syntax is:
```
priority_queue<type, vector<type>, greater<type>> pq;
```
For example,
```
// integers are arranged in ascending order
priority_queue<int, vector<int>, greater<int>> pq_integers;
```
In this type of priority queue, the smallest element gets the highest priority.
***
### Example: Min-Heap Priority Queue
```
#include<iostream>
#include <queue>
using namespace std;
int main() {
// create a priority queue of int // arranges elements in ascending order priority_queue<int, vector<int>, greater<int>> numbers;
// add items to priority_queue
numbers.push(1);
numbers.push(20);
numbers.push(7);
// print element with highest priority
cout << "Top element: " << numbers.top();
return 0;
}
```
**Output**
```
Top element: 1
``` |
| Shard | 35 (laksa) |
| Root Hash | 16786490165506891835 |
| Unparsed URL | com,programiz!www,/cpp-programming/priority-queue s443 |