- Include the necessary header file:
#include <queue>
- Define the comparator function or use the default comparator:
bool compare(int a, int b) {
return a < b; // Replace with custom comparison logic
}
- Declare the priority queue with the specified element type and comparator:
std::priority_queue<int, std::vector<int>, decltype(&compare)> pq(&compare);
- Insert elements into the priority queue:
pq.push(5);
pq.push(10);
pq.push(3);
- Access the top element of the priority queue:
int topElement = pq.top();
- Remove the top element from the priority queue:
pq.pop();