fork download
  1. //
  2. // main.cpp
  3. // Priority Queue (Min Heap)
  4. //
  5. // Created by Himanshu on 19/09/21.
  6. //
  7.  
  8. #include <iostream>
  9. #include <queue>
  10. #include <functional>
  11. using namespace std;
  12.  
  13. int main () {
  14.  
  15. priority_queue<int, vector< int>, greater <int> > pq;
  16.  
  17. cout<<"Insert Q(x) {30, 100, 25, 40}"<<endl;
  18. pq.push(30);
  19. pq.push(100);
  20. pq.push(25);
  21. pq.push(40);
  22.  
  23. cout<<"Minimum Element (Q)"<<endl;
  24. cout<<pq.top()<<endl;
  25.  
  26. cout<<"Minimum Element (Q) after Extract-Min (Q)"<<endl;
  27. pq.pop();
  28. cout<<pq.top()<<endl;
  29.  
  30. cout <<"Extracting out elements..."<<endl;
  31. while (!pq.empty()) {
  32. cout<< pq.top()<<" ";
  33. pq.pop();
  34. }
  35. cout<<endl;
  36.  
  37. if (pq.empty()) {
  38. cout<<"Priority queue is empty"<<endl;
  39. } else {
  40. cout<<"Priority queue is not empty"<<endl;
  41. }
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0.01s 5264KB
stdin
Standard input is empty
stdout
Insert Q(x) {30, 100, 25, 40}
Minimum Element (Q)
25
Minimum Element (Q) after Extract-Min (Q)
30
Extracting out elements...
30 40 100 
Priority queue is empty