fork download
  1. #include <iostream>
  2. #include <queue>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. cout << "Create queue containing values of type int" << endl;
  8. // We can create empty queue writing: queue<elements_type> variable_name;
  9. queue<int> qu;
  10.  
  11. // To get current size of the queue (number of elements in it) we use "size" method
  12. cout << "Size of the queue: " << qu.size() << endl;
  13.  
  14. cout << endl << "Add new elements to the end of the queue" << endl;
  15. // To add new elements to the queue we use "push" method
  16. // This places new element passed as a parameter at the end of the queue
  17. qu.push(5);
  18. qu.push(-50);
  19. qu.push(25);
  20. qu.push(120);
  21.  
  22. cout << "Size of the queue: " << qu.size() << endl;
  23.  
  24. // To get value of the first element in the queue we use "front" method
  25. // This method does not remove element from the queue
  26. cout << endl << "First element of the queue: " << qu.front() << endl;
  27. cout << "Removing top element from the queue" << endl;
  28. // To remove top element of the queue (from the first position) we use "pop" method
  29. // This method only removes the first element without returning its value
  30. qu.pop();
  31. cout << "First element of the queue: " << qu.front() << endl;
  32. cout << "Size of the queue: " << qu.size() << endl;
  33.  
  34. // To get value of the last element in the queue we use "back" method
  35. // This method does not remove element from the queue
  36. cout << endl << "Last element of the queue: " << qu.back() << endl;
  37.  
  38. cout << endl << "Clearing queue by assigning new value to it" << endl;
  39. qu = queue<int>();
  40.  
  41. cout << "Size of the queue: " << qu.size() << endl;
  42.  
  43. cout << endl << "Checking if queue is empty" << endl;
  44. // To check if queue is empty (its size is equal to zero) we can use "empty" method
  45. // This method returns true if queue is empty, false otherwise
  46. if (qu.empty()) {
  47. cout << "Queue is empty" << endl;
  48. } else {
  49. cout << "Queue is not empty" << endl;
  50. }
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0s 5636KB
stdin
Standard input is empty
stdout
Create queue containing values of type int
Size of the queue: 0

Add new elements to the end of the queue
Size of the queue: 4

First element of the queue: 5
Removing top element from the queue
First element of the queue: -50
Size of the queue: 3

Last element of the queue: 120

Clearing queue by assigning new value to it
Size of the queue: 0

Checking if queue is empty
Queue is empty