<stack> is a LIFO container adapter: you push on the top and pop from the top. It's built on top of a deque or vector and exposes only stack semantics.
Use it when you need a simple LIFO data structure; otherwise use a vector directly.
This example demonstrates push, pop, and top operations on a LIFO stack, showing last-in-first-out ordering.
// compile: g++ -std=c++11 -o stackexample stackexample.cpp // run: ./stackexample // description: LIFO stack operations #include <stack> #include <iostream> int main() { std::stack<int> st; st.push(1); st.push(2); st.push(3); std::cout << "top: " << st.top() << "\n"; st.pop(); std::cout << "after pop, top: " << st.top() << "\n"; std::cout << "size: " << st.size() << "\n"; return 0; }