In the C++ programming language, the priority_queue::top function is one of the most frequently utilized container adaptors that is provided by the Standard Template Library (STL). It provides access to the "largest" element based on the priority queue instead of the First-In-First-Out (FIFO) rule. It means that the container will always maintain the highest priority element at the "top" of the container.
In C++ , the top function is an important member function of priority queues that is commonly used to access the highest priority element in the priority queue without removing the element from the container. In a max heap, it would be the maximum element, while in a min heap (using a comparator like greater<T>), it would be the minimum element.
Syntax:
It has the following syntax:
priority_queue<data_type> pq;
pq.top();
Parameter: It does not take any parameters.
Return value: It returns a reference to the top element of the priority queue.
- In a max-heap priority queue (default), top returns the maximum element.
- In a min-heap priority queue (custom comparator using std::greater<T>), top returns the minimum element.
With the top function, we are not removing the highest priority element from the container. We are only returning a reference to the highest priority element in the container.
C++ Simple Example using priority_queue::top Function
Let us take an example to illustrate the priority_queue::top function in C++.
Example
#include <iostream>
#include <queue>
using namespace std; //using standard namespace
int main() //main function
{
int sum = 0;
priority_queue<int> pqueue;
//pushing value in pqueue.
pqueue.push(8);
pqueue.push(6);
pqueue.push(3);
pqueue.push(2);
pqueue.push(1);
cout<<pqueue.top();
return 0;
}
Output:
Explanation:
In this example, we have taken a priority_queue of integers that also contains multiple values. Since a priority queue is a max-heap, we use the top function to return the largest element stored in the queue.
C++ priority_queue::top Example Using Min-Heap
Let us take an example to illustrate the priority_queue::top function using a Min-Heap in C++.
Example
#include <iostream>
#include <queue>
using namespace std;
int main() {
priority_queue<int, vector<int>, greater<int>> minHeap;
minHeap.push(10);
minHeap.push(30);
minHeap.push(20);
minHeap.push(5);
cout << "The top element (minimum) is: " << minHeap.top() << endl;
return 0;
}
Output:
The top element (minimum) is: 5
Explanation:
In this example, we have taken a custom comparator greater<int> that provides the priority queue with min-heap behavior. After that, elements are pushed into the heap, and top returns the smallest element, which is 5, without removing it from the queue.
C++ Example to modify the Top Element of a Priority Queue
Let us take an example to modify the top elements of a priority queue in C++.
Example
#include <iostream>
#include <queue>
using namespace std;
int main() {
priority_queue<int> pq;
pq.push(50);
pq.push(40);
pq.push(60);
cout << "Original top: " << pq.top() << endl;
// Modify top element
int &topElement = const_cast<int&>(pq.top());
topElement = 100;
cout << "Modified top: " << pq.top() << endl;
return 0;
}
Output:
Original top: 60
Modified top: 100
Explanation:
In this example, we create a max-heap priorityqueue, and the top function is commonly utilized to access the largest element. After that, we use the constcast that allows us to modify the top element directly.
C++ priority_queue example with Pairs for Minimum Distance Selection
Priority queues are often utilized in graph algorithms. For example, in Dijkstra's algorithm , we rely on top to retrieve the smallest distance node. Let us take an example to illustrate the propriety_queue::top function to find the pair for Minimum Distance Selection in C++.
Example
#include <iostream>
#include <queue>
#include <vector>
using namespace std; //using standard namespace
int main() { //main function
// Min-heap for Dijkstra (distance, node)
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
pq.push({0, 1}); // distance = 0, node = 1
pq.push({2, 2});
pq.push({1, 3});
cout << "Node with smallest distance: " << pq.top().second << endl;
return 0;
}
Output:
Node with smallest distance: 1
Explanation:
In this example, the min-heap will ensure the pair with the smallest distance (0, node 1) will always be on top.
Features of priority_queue::top function in C++
There are several key features of the priority_queue::top function in C++. Some of them are as follows:
- In C++, the priority_queue::top function provide the constant-time (O(1)) access.
- It can perform operations differently for a max-heap and a min-heap.
- It doesn't allow us to remove the element from the queue.
- It returns the element with the highest priority in C++.
Conclusion
In conclusion, the priorityqueue::top function in C++ is a very useful function that gets the element with the highest priority in constant time. It means that the priorityqueue::top function will return the largest item in a max-heap, but we can use custom comparators to also return the smallest item. It is a simple and efficient function to implement scheduling systems, graph algorithms, event-driven simulations, and many other implementations.
C++ Priority_queue::top Function FAQs
1) What does priority_queue::top return in C++?
In the C++ programming language, the function top returns a reference to an element in the priority queue. It will return the highest priority element. In the case of using the priority queue as a max-heap, it will return the largest number. However, if a custom comparator such as greater is used, it will instead return the smallest number.
2) Does top remove the element from the priority queue in C++?
No. The top function only returns a reference to the highest priority element and does not modify or remove it. If we want to remove it, we must call the pop function. However, the top function is used to read that value, and pop is typically called right afterward to discard it.
3) What happens if we call top on an empty priority queue?
If we call the top function on an empty priority queue, there will be undefined behavior, which may crash the program. Therefore, we should always check if the queue is empty with the empty function before executing top.
4) Can we modify the value returned by the top function?
Yes, we can because top will return a reference, which allows us to modify the value directly. It modifies the value returned from top, potentially messing up the heap property of the priority queue, resulting in strange and unwanted behavior. It is much safer to pop and push with the modified value.
5) When is the top function useful?
In the C++ programming language, the top function is very helpful because it gives us a quick way to read the highest-priority element. It is very useful in algorithms like Dijkstra's shortest path, job scheduling, and event-driven systems.