STL

In this lesson, we'll see how to use C++ STL stack operations.

We'll cover the following

Library

While solving a problem, instead of implementing a stack every time, we’ll be using C++ STL Stack.

Include statement: #include <stack>

Below are some of the operations we’ll use frequently. For complete documentation of how they work, check here.

stack<int> S; //empty stack

int x = S.top(); // get top element
S.pop(); // pop top element
S.push(x); //push x on top

bool x = S.empty(); // to check if stack is empty
int sz = S.size(); // get size of stack

In the next lesson, we’ll see a solved problem using stacks.

Get hands-on with 1200+ tech skills courses.