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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- std::sort() in C++ STL
- Kruskal's Minimum Spanning Tree using STL in C++
- Dijkstra’s shortest path algorithm using set in STL
- Dijkstra's Shortest Path Algorithm using priority_queue of STL
- Prim's algorithm using priority_queue in STL
- unordered_map in C++ STL
- fill() and fill_n() functions in C++ STL
- std::transform() in C++ STL (Perform an operation on all elements)
- Count number of unique Triangles using STL | Set 1 (Using set)
- Permutations of a given string using STL
- Algorithm Library | C++ Magicians STL Algorithm
- Array algorithms in C++ STL (all_of, any_of, none_of, copy_n and iota)
- Iterators in C++ STL
- accumulate() and partial_sum() in C++ STL : numeric header
- std::partition in C++ STL
- Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge,
- Heap in C++ STL | make_heap(), push_heap(), pop_heap(), sort_heap(), is_heap, is_heap_until()
- numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
- Counts of distinct consecutive sub-string of length two using C++ STL
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.