C++ stacks
In C++, a stack is a container adaptor that operates on the Last In, First Out (LIFO) principle, meaning the last element added is the first one to be removed. It is implemented using the <stack> header in the C++ Standard Library and provides a simple interface for adding (pushing) and removing (popping) elements. The stack container is widely used in algorithms and problems involving backtracking, parsing, and maintaining a sequence of operations.
A stack supports the following basic operations:
To declare a stack:
std::stack<T> stack_name;
Where:
T
: The type of elements in the stack (e.g., int
, std::string
).You can also use custom containers as the underlying container for the stack (default is std::deque
).
std::stack<T, Container> stack_name;
Where:
Container
: The underlying container, such as std::vector
or std::deque
.
#include <iostream>
#include <stack>
int main() {
// Declare a stack of integers
std::stack<int> stack;
// Pushing elements into the stack
stack.push(10);
stack.push(20);
stack.push(30);
// Displaying the top element
std::cout << "Top element: " << stack.top() << std::endl; // Output: 30
return 0;
}
Explanation:
push(10)
, push(20)
, and push(30)
add the elements to the stack.top()
gives the top element of the stack without removing it.std::stack
push()
The push()
function adds an element to the top of the stack.
stack.push(40); // Adds 40 to the top of the stack
pop()
The pop()
function removes the element from the top of the stack. It does not return the removed element.
stack.pop(); // Removes the top element
top()
The top()
function returns the element at the top of the stack without removing it.
std::cout << "Top element: " << stack.top() << std::endl; // Displays the top element
size()
The size()
function returns the number of elements currently in the stack.
std::cout << "Stack size: " << stack.size() << std::endl;
empty()
The empty()
function checks whether the stack is empty.
if (stack.empty()) {
std::cout << "Stack is empty!" << std::endl;
} else {
std::cout << "Stack is not empty!" << std::endl;
}
Unlike other containers like vectors or lists, a stack does not allow direct iteration over its elements. To process the elements, you must pop them off one by one. Here's an example:
#include <iostream>
#include <stack>
int main() {
std::stack<int> stack;
stack.push(10);
stack.push(20);
stack.push(30);
// Popping elements from the stack
while (!stack.empty()) {
std::cout << stack.top() << " "; // Display the top element
stack.pop(); // Remove the top element
}
return 0;
}
Output:
30 20 10
Explanation:
top()
returns the element at the top of the stack.pop()
removes the element from the stack, allowing the next element to become the new top.The std::stack
container adaptor uses std::deque
by default as its underlying container, but you can choose to use other containers like std::vector
or std::list
as the base container.
std::vector
as the Underlying Container
#include <iostream>
#include <stack>
#include <vector>
int main() {
// Declare a stack using std::vector as the underlying container
std::stack<int, std::vector<int>> stack;
stack.push(10);
stack.push(20);
stack.push(30);
std::cout << "Top element: " << stack.top() << std::endl; // Output: 30
return 0;
}