#include#include int main() { std::stack myStack; myStack.push(1); myStack.push(2); myStack.push(3); std::cout << "Top element: " << myStack.top() << std::endl; return 0; }
#includeIn the above example, we again create a stack named 'myStack' and push elements 1, 2, and 3 onto it. We then loop through the stack while it is not empty, printing each element to the console and popping them off the stack. Package library:#include int main() { std::stack myStack; myStack.push(1); myStack.push(2); myStack.push(3); std::cout << "Popping elements: "; while(!myStack.empty()) { std::cout << myStack.top() << " "; myStack.pop(); } std::cout << std::endl; return 0; }