Why does Queue have front but Priority-queue has top in stl?
Why does the queue have front but the priority queue has top in stl?
The main difference between a queue and a priority queue is that a queue follows the FIFO (First-In-First-Out) principle, while a priority queue follows a specific priority order. In other words, the elements in a queue are processed in the order they were added, whereas the elements in a priority queue are processed based on their priority.
Queue:
A queue is a data structure that follows the FIFO (First-In-First-Out) principle, which means that the first element that is added to the queue is the first element that is removed. The elements in a queue are processed in the order they were added, and new elements are added to the back of the queue.
In the STL, the queue container is implemented as an adapter on top of other container classes, such as deque or list. The queue class provides a number of member functions, including the “front” member function, which returns a reference to the first element in the queue. This is the element that will be processed next when we call the “pop” member function to remove it from the queue.
For example: let’s say we have a queue of integers {1, 2, 3}. We can use the “front” member function to access the first element in the queue, which is 1:
C++
#include <bits/stdc++.h>using namespace std;int main(){ queue<int> myQueue; myQueue.push(1); myQueue.push(2); myQueue.push(3); // Accessing first element int firstElement = myQueue.front(); cout << firstElement << endl; return 0;} |
1
Time Complexity: O(1)
Auxiliary Space: O(1)
If we then call the “pop” member function to remove the first element from the queue, the next element in the queue (which is 2) becomes the first element, and we can access it using the “front” member function again:
C++
#include <bits/stdc++.h>using namespace std;int main(){ queue<int> myQueue; myQueue.push(1); myQueue.push(2); myQueue.push(3); myQueue.pop(); int newFirstElement = myQueue.front(); cout << newFirstElement << endl; return 0;} |
2
Time Complexity: O(1)
Auxiliary Space: O(1)
Priority Queue
A priority queue, on the other hand, is a data structure that orders elements based on their priority. The elements in a priority queue are processed in order of their priority, with the highest-priority element processed first. New elements are added to the priority queue based on their priority order, and the highest-priority element is always at the front of the queue.
In the STL, the priority queue container is implemented as an adapter on top of a vector or a deque, and it requires a comparison function to determine the priority order of elements. The priority queue class provides a number of member functions, including the “top” member function, which returns a reference to the element with the highest priority in the queue. This is the element that will be processed next when we call the “pop” member function to remove it from the queue.
For example, let’s say we have a priority queue (Max-Heap) of integers {3, 1, 2}. We can use the “top” member function to access the element with the highest priority, which is 3:
C++
#include <bits/stdc++.h>using namespace std;int main(){ priority_queue<int> myPriorityQueue; myPriorityQueue.push(3); myPriorityQueue.push(1); myPriorityQueue.push(2); int highestPriorityElement = myPriorityQueue.top(); cout << highestPriorityElement << endl; return 0;} |
3
Time Complexity: O(logN), Where N is the number of elements to be pushed.
Auxiliary Space: O(1)
If we then call the “pop” member function to remove the highest-priority element from the queue, the next element with the highest priority (which is 2) becomes the new top element, and we can access it using the “top” member function again:
C++
#include <bits/stdc++.h>using namespace std;int main(){ priority_queue<int> myPriorityQueue; myPriorityQueue.push(3); myPriorityQueue.push(1); myPriorityQueue.push(2); myPriorityQueue.pop(); int newHighestPriorityElement = myPriorityQueue.top(); cout << newHighestPriorityElement << endl; return 0;} |
2
Time Complexity: O(logN), Where N is the number of elements to be pushed or poped.
Auxiliary Space: O(1)


Please Login to comment...