priority_queue::top() in C++ STL
Last Updated :
31 Jul, 2022
Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. In general, elements are arranged according to some priority. However in C++ STL, the top element is the greatest element by default. We can also make a priority queue with a minimum element at the top by passing an additional parameter while creating the priority queue. It is similar to min/max heap.
priority_queue::top()
top() function is used to reference the top element of the priority queue. The top (by default) element is the largest element in C++ STL.
However in other programming languages like java, by default a min heap is created and the top() gives the minimum element.
Syntax :
pqueuename.top()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the top(by default it is the largest element)
element of the priority queue container.
Examples:
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
CPP
// CPP program to illustrate
// Implementation of top() function
#include <iostream>
#include <queue>
using namespace std;
int main()
{
priority_queue<int> pqueue;
pqueue.push(5);
pqueue.push(1);
pqueue.push(7);
// Priority queue top
cout << pqueue.top();
return 0;
}
Output:
7
Time Complexity: O(1)
Auxiliary Space: O(n)
Application : 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
Algorithm 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).
CPP
// CPP program to illustrate
// Application of top() function
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int prime = 0, nonprime = 0, size;
priority_queue<int> pqueue;
pqueue.push(1);
pqueue.push(8);
pqueue.push(3);
pqueue.push(6);
pqueue.push(2);
size = pqueue.size();
// Priority queue becomes 1, 8, 3, 6, 2
while (!pqueue.empty()) {
for (int i = 2; i <= pqueue.top() / 2; ++i) {
if (pqueue.top() % i == 0) {
prime++;
break;
}
}
pqueue.pop();
}
cout << "Prime - " << prime << endl;
cout << "Non Prime - " << size - prime;
return 0;
}
Output:
Prime - 2
Non Prime - 3
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
Similar Reads
Priority Queue in C++ STL In C++, priority queue is a type of queue in which there is some priority assigned to the elements. According to this priority, elements are removed from the queue. By default, the value of the element being inserted is considered as priority. Higher its value, higher its priority. But this can be c
6 min read
priority_queue::push() and priority_queue::pop() in C++ STL In C++, priority_queue::push() and priority_queue::pop() methods are used to insert and delete the element from the priority_queue container. They both are the member functions of std::priority_queue class defined inside <queue> header file. In this article, we will learn about priority_queue:
2 min read
priority_queue::top() in C++ STL Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. In general, elements are arranged according to some priority. However in C++ STL, the top element is the greatest elem
3 min read
priority_queue::empty() and priority_queue::size() in C++ STL Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the sma
4 min read
priority_queue emplace() in C++ STL Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the sma
4 min read
priority_queue::swap() in C++ STL Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the sma
3 min read
priority_queue value_type in C++ STL The priority_queue :: value_type method is a built-in function in C++ STL which represents the type of object stored as an element in a priority_queue. It acts as a synonym for the template parameter. Time complexity: O(1)Syntax: priority_queue::value_type variable_name It has no parameters and no r
2 min read
Priority Queue of Sets in C++ with Examples Priority Queues 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 nonincreasing order (hence we can see that each element of the queue has a priority {fixed order}). Functi
4 min read
Priority queue of pairs in C++ with ordering by first and second element Priority Queue: Priority queue is the extension of the queue in which elements associated with priority and elements having higher priority is popped first. Priority queue can contain elements with various data types such as integer, pair of integers, custom data type. But one thing is common that t
5 min read
Multiple comparisons in a C++ priority queue? What is a Priority Queue? A Priority Queue is an abstract data type that is similar to a queue, and every element has some priority value associated with it. The priority of the elements in a priority queue determines the order in which elements are served (i.e., the order in which they are removed)
5 min read