The `priority_queue` is a container adapter in the C++ Standard Template Library (STL) that provides a queue implementation based on priority. This means that elements are sorted according to their priority (by default, it's a max heap where the element with the highest priority comes first), and insertions occur at one end of the queue while removals occur at the other.
The `priority_queue` uses an underlying container (default is `vector`) to store elements and offers a set of functions to access and modify the queue. Let's delve into the basic usage and additional sorting methods for `priority_queue`.
Basic Usage
Including the Header
Firstly, to use `priority_queue`, you need to include the queue header:
#include <queue>
Creating a `priority_queue`
You can create a `priority_queue` as follows:
std::priority_queue<int> pq; // Default is a max heap
If you prefer a min heap, you can specify the second template parameter:
std::priority_queue<int, std::vector<int>, std::greater<int>> minHeap;
Inserting Elements
To insert elements into the priority queue, use the `push` function:
pq.push(10);
pq.push(5);
pq.push(20);
Accessing the Top Element
To access the top element of the queue (the element with the highest priority), use the `top` function:
int topElement = pq.top();
Deleting the Top Element
To remove the top element from the queue, use the `pop` function:
pq.pop();
Additional Sorting Methods
If you want to use a custom sorting method, you can provide a comparison function.
Using Lambda Functions
std::priority_queue<int, std::vector<int>, [](int a, int b) { return a > b; }> customQueue;
Using Function Objects
struct Compare {
bool operator()(int a, int b) {
return a > b;
}
};
std::priority_queue<int, std::vector<int>, Compare> customQueue;
Here, `Compare` is a function object that defines an overloaded operator() to compare the priority of two elements. You can modify the implementation of the comparison function to meet specific sorting needs.
Customizing `priority_queue` with Classes or Structs(the most interetsing one)
As pointed out by the user, if you are using `priority_queue` to store custom classes or structs, you can define the comparison function or operator within the class or struct itself, without providing an additional comparison function in the `priority_queue` template parameters.
Here's an example of how to define a comparison operator within a class or struct for use in `priority_queue`:
#include <iostream>
#include <queue>
class CustomObject {
public:
int value;
// Constructor
CustomObject(int val) : value(val) {}
// Overload less than operator for priority definition
bool operator<(const CustomObject& other) const {
// Define the comparison rule as needed
return value < other.value;
}
};
int main() {
// Priority queue with custom objects
std::priority_queue<CustomObject> customQueue;
// Insert elements
customQueue.push(CustomObject(10));
customQueue.push(CustomObject(5));
customQueue.push(CustomObject(20));
// Access and delete top element
std::cout << "Top element: " << customQueue.top().value << std::endl;
customQueue.pop();
// Output remaining elements
while (!customQueue.empty()) {
std::cout << customQueue.top().value << " ";
customQueue.pop();
}
return 0;
}
In this example, the `CustomObject` class overloads the less than operator to sort elements in the `priority_queue` based on the `value`. This approach makes it easier to use custom classes or structs and provides a clearer comparison logic.
In summary, `priority_queue` offers a convenient interface to implement priority-based queues and can be tailored to different sorting needs by providing custom comparison functions.