maxheap cpp stl

  1. Include the necessary header file for using the priority queue in C++:
#include <queue>
  1. 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
    }
};
  1. Declare a priority queue using the custom comparator:
std::priority_queue<int, std::vector<int>, MaxHeapComparator> maxHeap;
  1. Add elements to the max heap:
maxHeap.push(3);
maxHeap.push(8);
maxHeap.push(1);
  1. Access the maximum element in the max heap:
int maxElement = maxHeap.top();
  1. Remove the maximum element from the max heap:
maxHeap.pop();