Priority Queue in C++ Standard Template Library (STL)
Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements are in non decreasing order(hence we can see that each element of the queue has a priority{fixed order}).
Methods of priority queue are:
- priority_queue::empty() in C++ STL– empty() function returns whether the queue is empty.
- priority_queue::size() in C++ STL– size() function returns the size of the queue.
- priority_queue::top() in C++ STL– Returns a reference to the top most element of the queue
- priority_queue::push() in C++ STL– push(g) function adds the element ‘g’ at the end of the queue.
- priority_queue::pop() in C++ STL– pop() function deletes the first element of the queue.
- priority_queue::swap() in C++ STL– This function is used to swap the contents of one priority queue with another priority queue of same type and size.
- priority_queue::emplace() in C++ STL – This function is used to insert a new element into the priority queue container, the new element is added to the top of the priority queue.
- priority_queue value_type in C++ STL– Represents the type of object stored as an element in a priority_queue. It acts as a synonym for the template parameter.
Below is the C++ program to show the functionality of Priority Queue:
#include <iostream> #include <queue> using namespace std; void showpq(priority_queue <int> gq) { priority_queue <int> g = gq; while (!g.empty()) { cout << '\t' << g.top(); g.pop(); } cout << '\n'; } int main () { priority_queue <int> gquiz; gquiz.push(10); gquiz.push(30); gquiz.push(20); gquiz.push(5); gquiz.push(1); cout << "The priority queue gquiz is : "; showpq(gquiz); cout << "\ngquiz.size() : " << gquiz.size(); cout << "\ngquiz.top() : " << gquiz.top(); cout << "\ngquiz.pop() : "; gquiz.pop(); showpq(gquiz); return 0; } |
chevron_right
filter_none
Output:
The priority queue gquiz is : 30 20 10 5 1 gquiz.size() : 5 gquiz.top() : 30 gquiz.pop() : 20 10 5 1
Recent articles on priority queue in STL
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- Queue in Standard Template Library (STL)
- The C++ Standard Template Library (STL)
- Set in C++ Standard Template Library (STL)
- Map in C++ Standard Template Library (STL)
- Deque in C++ Standard Template Library (STL)
- List in C++ Standard Template Library (STL)
- Pair in C++ Standard Template Library (STL)
- Multiset in C++ Standard Template Library (STL)
- Multimap in C++ Standard Template Library (STL)
- Sort in C++ Standard Template Library (STL)
- Binary Search in C++ Standard Template Library (STL)
- Priority Queue in Python
- Priority Queue | Set 1 (Introduction)
- Applications of Priority Queue
- STL Priority Queue for Structure or Class
Improved By : cr7_bullet



