- Include the necessary header file for using the priority queue in C++:
#include <queue>
- Define a custom comparator function for creating a max heap:
struct MaxHeapComparator {
bool operator()(int a, int b) {
return a < b; // Change to a > b for max heap
}
};
- Declare a priority queue using the custom comparator:
std::priority_queue<int, std::vector<int>, MaxHeapComparator> maxHeap;
- Add elements to the max heap:
maxHeap.push(3);
maxHeap.push(8);
maxHeap.push(1);
- Access the maximum element in the max heap:
int maxElement = maxHeap.top();
- Remove the maximum element from the max heap:
maxHeap.pop();