Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only.
push() function is used to insert an element at the top of the stack. The element is added to the stack container and the size of the stack is increased by 1.
Syntax :
stackname.push(value) Parameters : The value of the element to be inserted is passed as the parameter. Result : Adds an element of value same as that of the parameter passed at the top of the stack.
Examples:
Input : mystack
mystack.push(6);
Output : 6
Input : mystack
mystack.push(0);
mystack.push(1);
Output : 0, 1
Errors and Exceptions
1. Shows error if the value passed doesn’t match the stack type.
2. Shows no exception throw guarantee if the parameter doesn’t throw any exception.
// CPP program to illustrate // Implementation of push() function #include <iostream> #include <stack> using namespace std; int main() { // Empty stack stack<int> mystack; mystack.push(0); mystack.push(1); mystack.push(2); // Printing content of stack while (!mystack.empty()) { cout << ' ' << mystack.top(); mystack.pop(); } } |
Output:
2 1 0 Note that output is printed on the basis of LIFO property
pop() function is used to remove an element from the top of the stack(newest element in the stack). The element is removed to the stack container and the size of the stack is decreased by 1.
Syntax :
stackname.pop() Parameters : No parameters are passed. Result : Removes the newest element in the stack or basically the top element.
Examples:
Input : mystack = 0, 1, 2
mystack.pop();
Output : 0, 1
Input : mystack = 0, 1, 2, 3, 4, 5
mystack.pop();
Output : 0, 1, 2, 3, 4
Errors and Exceptions
1. Shows error if a parameter is passed.
2. Shows no exception throw guarantee.
// CPP program to illustrate // Implementation of pop() function #include <iostream> #include <stack> using namespace std; int main() { stack<int> mystack; mystack.push(1); mystack.push(2); mystack.push(3); mystack.push(4); // Stack becomes 1, 2, 3, 4 mystack.pop(); mystack.pop(); // Stack becomes 1, 2 while (!mystack.empty()) { cout << ' ' << mystack.top(); mystack.pop(); } } |
Output:
2 1 Note that output is printed on the basis of LIFO property
Application :
Given a number of integers, add them to the stack and find the size of the stack without using size function.
Input : 5, 13, 0, 9, 4 Output: 5
Algorithm
1. Push the given elements to the stack container one by one.
2. Keep popping the elements of stack until it becomes empty, and increment the counter variable.
3. Print the counter variable.
// CPP program to illustrate // Application of push() and pop() function #include <iostream> #include <stack> using namespace std; int main() { int c = 0; // Empty stack stack<int> mystack; mystack.push(5); mystack.push(13); mystack.push(0); mystack.push(9); mystack.push(4); // stack becomes 5, 13, 0, 9, 4 // Counting number of elements in queue while (!mystack.empty()) { mystack.pop(); c++; } cout << c; } |
Output:
5
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- queue::push() and queue::pop() in C++ STL
- priority_queue::push() and priority_queue::pop() in C++ STL
- stack empty() and stack size() in C++ STL
- Sort a stack using a temporary stack
- Stack in C++ STL
- stack swap() in C++ STL
- stack top() in C++ STL
- stack emplace() in C++ STL
- Stack of Pair in C++ STL with Examples
- Design a stack that supports getMin() in O(1) time and O(1) extra space
- Heap overflow and Stack overflow
- Level order traversal in spiral form | Using one stack and one queue
- Level order traversal in spiral form using stack and multimap
- Design and Implement Special Stack Data Structure | Added Space Optimized Version
- Stack Unwinding in C++
- Check if the elements of stack are pairwise sorted
- Sudo Placement[1.3] | Stack Design
- Delete middle element of a stack
- Sort a stack using recursion
- fill() and fill_n() functions in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

