Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is the greatest of all elements in the queue.
top() function is used to reference the top(or the largest) element of the priority queue.
pqueuename.top() Parameters : No value is needed to pass as the parameter. Returns : Direct reference to the top(or the largest) element of the priority queue container.
Input : pqueue.push(5); pqueue.push(1); pqueue.top(); Output : 5 Input : pqueue.push(5); pqueue.push(1); pqueue.push(7); pqueue.top(); Output : 7
Errors and Exceptions
1. If the priority queue container is empty, it causes undefined behaviour
2. It has a no exception throw guarantee if the priority queue is not empty
Given a priority queue of integers, find the number of prime and non prime numbers.
Input : 8, 6, 3, 2, 1 Output: Prime - 2 Non Prime - 3
1. Enter the size of the priority queue into a variable.
2. Check if the priority queue is empty, check if the top element is prime, if prime increments the prime counter, and pop the top element.
3. Repeat this step until the priority queue is empty.
4. Print the final value of the variable prime and nonprime(size – prime).
Prime - 2 Non Prime - 3
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- Find the maximum cost path from the bottom-left corner to the top-right corner
- C++ program to compare two Strings using Operator Overloading
- not1 and not2 function templates in C++ STL with Examples
- Go vs C++
- Trie Data Structure using smart pointer and OOP in C++
- Modular Exponentiation of Complex Numbers
- std::hash class in C++ STL
- History of C++
- What are Forward declarations in C++
- __builtin_inf() functions of GCC compiler
- Structures in C++
- Variables in C++
- Implementing Forward Iterator in BST
- Count substrings that contain all vowels | SET 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.