fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. #include <queue>
  5. using namespace std;
  6.  
  7. int main() {
  8.  
  9. // (VECTOR PART) //
  10.  
  11. cout << "VECTOR\n";
  12.  
  13. vector<int> v;
  14.  
  15. // push_back(x)
  16. v.push_back(10);
  17. v.push_back(20);
  18. v.push_back(30);
  19.  
  20. // size()
  21.  
  22. cout << "Size: " << v.size() << endl;
  23.  
  24. // begin() and end()
  25.  
  26. cout << "Elements: ";
  27. for(auto it = v.begin(); it != v.end(); it++) {
  28. cout << *it << " ";
  29. }
  30. cout << endl;
  31.  
  32. // pop_back()
  33.  
  34. v.pop_back();
  35.  
  36. // insert()
  37.  
  38. v.insert(v.begin() + 1, 15);
  39.  
  40. // erase()
  41.  
  42. v.erase(v.begin());
  43.  
  44. cout << "After operations: ";
  45. for(int i = 0; i < v.size(); i++) {
  46. cout << v[i] << " ";
  47. }
  48. cout << endl;
  49.  
  50. // empty()
  51.  
  52. cout << "Is empty? " << (v.empty() ? "Yes" : "No") << endl;
  53.  
  54. // clear()
  55.  
  56. v.clear();
  57. cout << "Size after clear: " << v.size() << endl;
  58.  
  59.  
  60. // (STACK PART) //
  61.  
  62. cout << "\nSTACK\n";
  63.  
  64. stack<int> st;
  65.  
  66. // push(x)
  67.  
  68. st.push(5);
  69. st.push(10);
  70. st.push(15);
  71.  
  72. // top()
  73.  
  74. cout << "Top element: " << st.top() << endl;
  75.  
  76. // size()
  77.  
  78. cout << "Size: " << st.size() << endl;
  79.  
  80. // pop()
  81.  
  82. st.pop();
  83. cout << "Top after pop: " << st.top() << endl;
  84.  
  85. // empty()
  86.  
  87. cout << "Is empty? " << (st.empty() ? "Yes" : "No") << endl;
  88.  
  89.  
  90. // (QUEUE PART) //
  91.  
  92. cout << "\nQUEUE\n";
  93.  
  94. queue<int> q;
  95.  
  96. // push(x)
  97.  
  98. q.push(100);
  99. q.push(200);
  100. q.push(300);
  101.  
  102. // front() and back()
  103.  
  104. cout << "Front: " << q.front() << endl;
  105. cout << "Back: " << q.back() << endl;
  106.  
  107. // size()
  108.  
  109. cout << "Size: " << q.size() << endl;
  110.  
  111. // pop()
  112.  
  113. q.pop();
  114. cout << "Front after pop: " << q.front() << endl;
  115.  
  116. // empty()
  117.  
  118. cout << "Is empty? " << (q.empty() ? "Yes" : "No") << endl;
  119.  
  120. return 0;
  121. }
  122.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
VECTOR
Size: 3
Elements: 10 20 30 
After operations: 15 20 
Is empty? No
Size after clear: 0

STACK
Top element: 15
Size: 3
Top after pop: 10
Is empty? No

QUEUE
Front: 100
Back: 300
Size: 3
Front after pop: 200
Is empty? No