fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. class heap
  8. {
  9. private:
  10. vector<int> storage;
  11. public:
  12. heap(){}
  13. heap(const vector<int>& vec):storage(vec){make_heap(storage.begin(),storage.end());}
  14. void push(int n)
  15. {
  16. storage.push_back(n);
  17. push_heap(storage.begin(),storage.end());
  18. }
  19. int pop()
  20. {
  21. pop_heap(storage.begin(),storage.end());
  22. int high=storage.back();
  23. storage.pop_back();
  24. return high;
  25. }
  26. bool isempty()const
  27. {
  28. return storage.empty();
  29. }
  30. void print()const
  31. {
  32. for (auto e:storage)
  33. cout <<e <<" ";
  34. }
  35. };
  36.  
  37. int main() {
  38. vector <int> test = {1,4,2,5,7,3};
  39. heap h=test;
  40. h.print();cout <<endl;
  41. h.push(4);
  42. h.print();cout <<endl;
  43. h.push(9);
  44. h.print();
  45. while (!h.isempty())
  46. {
  47. cout <<"pop " <<h.pop() <<endl;
  48. h.print();
  49. }
  50. return 0;
  51. }
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
7 5 3 1 4 2 
7 5 4 1 4 2 3 
9 7 4 5 4 2 3 1 pop 9
7 5 4 1 4 2 3 pop 7
5 4 4 1 3 2 pop 5
4 4 2 1 3 pop 4
4 3 2 1 pop 4
3 1 2 pop 3
2 1 pop 2
1 pop 1