fork download
  1. #include <iostream>
  2. #include <queue>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. // Create a priority queue of integers
  8. priority_queue<int> pq;
  9.  
  10. // Add elements to the priority queue
  11. pq.push(10);
  12. pq.push(20);
  13. pq.push(30);
  14. pq.push(40);
  15.  
  16. // Print the top element of the priority queue
  17. cout << "Top element: " << pq.top() << endl;
  18.  
  19. // Remove the top element from the priority queue
  20. pq.pop();
  21.  
  22. // Print the top element of the priority queue again
  23. cout << "Top element after pop: " << pq.top() << endl;
  24.  
  25. // Check if the priority queue is empty
  26. if (pq.empty()) {
  27. cout << "Priority queue is empty" << endl;
  28. } else {
  29. cout << "Priority queue is not empty" << endl;
  30. }
  31.  
  32. // Print the size of the priority queue
  33. cout << "Size of priority queue: " << pq.size() << endl;
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
Top element: 40
Top element after pop: 30
Priority queue is not empty
Size of priority queue: 3