C++ Stacks
In C++, a stack is a useful data structure that stores elements in a last-in-first-out (LIFO) order. This means the last element added to the stack is the first one to be removed.
Stacks are implemented as a container adaptor, a class that uses another container class as its underlying container. The underlying container class can be a vector, deque, or list.
Creating a Stack
To create a stack in C++, the <stack> header and the std namespace need to be included:
#include <stack>
using namespace std;
stack<dataType> stackName;
In the syntax:
dataType: The data type of the elements to be stored in the stack.stackName: The name of the stack.
For example:
#include <stack>using namespace std;// Create a stack of integersstack<int> myStack;
Adding Elements to a Stack
The .push() method can be used to append an element to the top of a stack:
#include <iostream>#include <stack>using namespace std;int main() {// Create a stack of integersstack<int> myStack;// Add elements to the stackmyStack.push(10);myStack.push(20);myStack.push(30);// Print the elements of the stackwhile(!myStack.empty()) {cout << myStack.top() << "\n";myStack.pop();}return 0;}
Here is the output:
302010
Accessing Elements in a Stack
The .top() method can be used to access the top element of a stack:
#include <iostream>#include <stack>using namespace std;int main() {// Create a stack of integersstack<int> myStack;// Add elements to the stackmyStack.push(10);myStack.push(20);myStack.push(30);// Print the top element of the stackcout << myStack.top() << "\n";return 0;}
Here is the output:
30
Removing Elements from a Stack
The .pop() method can be used to delete or remove an element from the top of a stack:
#include <iostream>#include <stack>using namespace std;int main() {// Create a stack of integersstack<int> myStack;// Add elements to the stackmyStack.push(10);myStack.push(20);myStack.push(30);// Remove the top element of the stackmyStack.pop();// Print the elements of the stackwhile(!myStack.empty()) {cout << myStack.top() << "\n";myStack.pop();}return 0;}
Here is the output:
2010
Getting the Size of a Stack
The .size() method can be used to get the size of a stack:
#include <iostream>#include <stack>using namespace std;int main() {// Create a stack of integersstack<int> myStack;// Add elements to the stackmyStack.push(10);myStack.push(20);myStack.push(30);// Get the size of the stackcout << myStack.size() << "\n";return 0;}
Here is the output:
3
Checking if a Stack is Empty
The .empty() method is used for checking if a stack is empty. If it is, the method returns 1. Otherwise, it returns 0:
Frequently Asked Questions
1. What is the time complexity of stack operations in C++?
All major stack operations—.push(), .top(), .pop(), .size(), and .empty()—have constant time complexity O(1).
2. Is std::stack thread-safe?
No, std::stack is not thread-safe. You must use synchronization (like mutex) in multithreaded applications.
3. When should I use a stack in C++?
Stacks are ideal for:
- Reversing data
- Undo features
- Expression evaluation
- Depth-first search (DFS)
- Function call tracking (Recursion)
Stacks
- .empty()
- Returns true if the stack has no elements.
- .pop()
- Removes the last item added to the top of the stack.
- .push()
- Adds an element to the top of the stack.
- .size()
- Returns the number of elements in the stack.
- .top()
- Returns the element on the top of the stack.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours