Does STL priority queue allow duplicate values?
Yes, in C++ priority_queue, we may have duplicate values.
// C++ program to demonstrate that duplicate // values are allowed in a priority queue // (with maximum value at the top) #include <bits/stdc++.h> using namespace std; int main() { priority_queue< int > pq; pq.push(30); pq.push(5); pq.push(30); cout << pq.top() << " " ; pq.pop(); cout << pq.top() << " " ; pq.pop(); return 0; } |
chevron_right
filter_none
Output:
30 30
// C++ program to demonstrate that duplicate // values are allowed in a priority queue // (with minimum value at the top) #include <bits/stdc++.h> using namespace std; int main() { priority_queue< int > pq; pq.push(5); pq.push(5); pq.push(5); cout << pq.top() << " " ; pq.pop(); cout << pq.top() << " " ; pq.pop(); cout << pq.top() << " " ; pq.pop(); return 0; } |
chevron_right
filter_none
Output:
5 5 5
Recommended Posts:
- STL Priority Queue for Structure or Class
- Priority queue of pairs in C++ (Ordered by first)
- Priority Queue in C++ Standard Template Library (STL)
- queue::empty() and queue::size() in C++ STL
- queue::front() and queue::back() in C++ STL
- queue::push() and queue::pop() in C++ STL
- Remove duplicate elements in an Array using STL in C++
- Find and print duplicate words in std::vector<string> using STL functions
- queue::emplace() in C++ STL
- queue::swap() in C++ STL
- Queue using Stacks
- Queue in Standard Template Library (STL)
- Check if X can give change to every person in the Queue
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- How to traverse through all values for a given key in multimap?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.