Priority Queue in STL 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 non increasing order(hence we can see that each element of the queue has a priority{fixed order})
Vector in STL Vector is same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators.
Priority Queue of Vectors in STL: Priority Queue of Vectors can be very efficient in designing complex data structures.
Syntax:
priority_queue<vector<datatype>> pq;
For example: Consider a simple problem where we have to print the maximum vector that is in the queue.
// C++ program to demonstrate // use of priority queue for vectors #include <bits/stdc++.h> using namespace std; priority_queue<vector<int> > pq; // Prints maximum vector void Print_Maximum_Vector(vector<int> Vec) { for (int i = 0; i < Vec.size(); i++) { cout << Vec[i] << " "; } cout << endl; return; } // Driver code int main() { // Initializing some vectors vector<int> data_1{ 10, 20, 30, 40 }; vector<int> data_2{ 10, 20, 35, 40 }; vector<int> data_3{ 30, 25, 10, 50 }; vector<int> data_4{ 20, 10, 30, 40 }; vector<int> data_5{ 5, 10, 30, 40 }; // Inserting vectors into priority queue pq.push(data_1); pq.push(data_2); // printing the maximum vector till now Print_Maximum_Vector(pq.top()); // Inserting vectors into priority queue pq.push(data_3); // printing the maximum vector till now Print_Maximum_Vector(pq.top()); // Inserting vectors into priority queue pq.push(data_4); pq.push(data_5); // printing the maximum vector till now Print_Maximum_Vector(pq.top()); return 0; } |
10 20 35 40 30 25 10 50 30 25 10 50
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.
Recommended Posts:
- Difference between Circular Queue and Priority Queue
- Applications of Priority Queue
- Priority Queue | Set 1 (Introduction)
- Priority Queue in Python
- STL Priority Queue for Structure or Class
- Does STL priority queue allow duplicate values?
- Priority Queue using Linked List
- Priority queue of pairs in C++ (Ordered by first)
- Priority queue of pairs in C++ with ordering by first and second element
- Priority Queue using doubly linked list
- Priority Queue in C++ Standard Template Library (STL)
- Find the K closest points to origin using Priority Queue
- CPU Scheduling in Operating Systems using priority queue with gantt chart
- Set of Vectors in C++ STL with Examples
- Vector of Vectors in C++ STL with Examples
- Queue of Pairs in C++ STL with Examples
- Stack and Queue in Python using queue Module
- Check if a queue can be sorted into another queue using a stack
- How to join two Vectors using STL in C++?
- Array of Vectors in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

