fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Comp
  5. {
  6. public:
  7. bool operator()(const pair<int, int> &a, const pair<int, int> &b)
  8. {
  9. cout << "Custom comparator called" << endl;
  10. return a.second > b.second;
  11. }
  12. };
  13.  
  14. template<typename T>
  15. class custom_priority_queue_MinHeap : public std::priority_queue<T, std::vector<T>, Comp>
  16. {
  17. public:
  18. bool remove(const T& value)
  19. {
  20. auto it = std::find(this->c.begin(), this->c.end(), value);
  21. if (it != this->c.end())
  22. {
  23. this->c.erase(it);
  24. std::make_heap(this->c.begin(), this->c.end(), this->comp);
  25. return true;
  26. }
  27.  
  28. return false;
  29. }
  30.  
  31. bool operator()(const pair<int, int> &a, const pair<int, int> &b)
  32. {
  33. cout << "Custom comparator called" << endl;
  34. return a.second > b.second;
  35. }
  36. };
  37.  
  38. int main() {
  39. custom_priority_queue_MinHeap<pair<int, int>> minHeap;
  40. minHeap.push({0, 10});
  41. minHeap.push({1, 5});
  42. minHeap.push({2, 15});
  43.  
  44. while(!minHeap.empty())
  45. {
  46. pair<int, int> p = minHeap.top();
  47. minHeap.pop();
  48. cout << p.first << " " << p.second << endl;
  49. }
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0s 4536KB
stdin
Standard input is empty
stdout
Custom comparator called
Custom comparator called
Custom comparator called
1 5
0 10
2 15