fork download
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. int main() {
  5. std::stack<int> myStack;
  6.  
  7. // A) Add at least 10 elements into the stack
  8. for (int i = 1; i <= 10; ++i) {
  9. myStack.push(i);
  10. }
  11.  
  12. // B) Remove an element from the stack
  13. if (!myStack.empty()) {
  14. myStack.pop();
  15. }
  16.  
  17. // C) Display the content of the stack
  18. std::stack<int> tempStack = myStack; // Copy to a temporary stack to display
  19. std::cout << "Stack contents: ";
  20. while (!tempStack.empty()) {
  21. std::cout << tempStack.top() << " ";
  22. tempStack.pop();
  23. }
  24. std::cout << std::endl;
  25.  
  26. return 0;
  27. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Stack contents: 9 8 7 6 5 4 3 2 1