Labs ICT
Pro Login

Stack

1 min read | C++ Tutorial

Want the full learning experience?

Get structured courses, certificates, projects, and instructor support with LabsICT Pro.

Explore Pro Courses

C++ Stack

std::stack follows the LIFO (Last In, First Out) principle. Elements are added with push(), removed with pop(), and the top element is accessed with top(). Stacks are useful for undo operations, expression evaluation, and depth-first traversal.

#include <iostream>
#include <stack>

int main() {
    std::stack<int> s;
    s.push(10);
    s.push(20);
    s.push(30);
    std::cout << "Top: " << s.top() << "\n";
    s.pop();
    std::cout << "Top after pop: " << s.top();
    return 0;
}