How to Push an Element into Stack in C++? Last Updated : 04 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++ Stacks are a type of container adaptor with LIFO(Last In First Out) type of work, where a new element is added at one end (top) and an element is removed from that end only. In this article, we will learn to push the elements onto a Stack in C++. Example:Input:myStack = {40};Output:myStack: {40, 30, 20, 10};Insert Elements in a Stack in C++To push an element onto a std::stack in C++, we can use the std::stack::push() function, which inserts or pushes the element at the top of the stack. This is the inbuilt function that is provided by the C++ inside the std::stack class template. C++ Program to Push an Element into a Stack C++ // C++ Program to push an elements onto a stack #include <iostream> #include <stack> using namespace std; // Driver Code int main() { // Creating empty Stack stack<int> stackData; // Pushing elements to the stack stackData.push(10); stackData.push(20); stackData.push(30); stackData.push(40); // Printing the updated stack cout << "Stack: "; while (!stackData.empty()) { cout << stackData.top(); stackData.pop(); if (!stackData.empty()) { cout << ", "; } } cout << endl; return 0; } OutputStack: 40, 30, 20, 10 Time Complexity: O(N), where N is the number of elements to be inserted.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Push an Element into Stack in C++? G gauravgandal Follow Improve Article Tags : C++ Programs C++ STL cpp-stack CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Pop an Element From a Stack in C++? In C++, stacks are used to store a collection of similar types of data in a Last-In-First-Out (LIFO) manner. In this article, we will discuss how to pop an element from a stack in C++. Example:Input: myStack = {10, 34, 12, 90, 1}; Output: myStack = {10, 34, 12, 90};Removing an Element from a Stack i 2 min read How to Check if a Stack is Empty in C++? In C++, we have a stack data structure that follows a LIFO (Last In First Out) rule of operation. In this article, we will learn how to check if a stack is empty in C++. Example:Input:myStack = {1, 2, 3 } Output:Stack is not EmptyChecking if a Stack is Empty in C++To check if a stack is empty in C++ 2 min read How to Create a Stack of Pairs in C++? In C++, Stacks are a type of container adaptor with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. A pair is a simple container that stores data in a key and value format. In this article, we will learn how to crea 2 min read How to Create a Stack of Set in C++? In C++ STL, Stacks are a type of container adaptor with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. Sets are a type of associative container in which each element is unique and in some sorted order. In this arti 2 min read How to Add Elements in a Vector in C++? In C++, vector provides several built-in methods to insert the elements and efficiency of the insertion depends on the position where the insertion takes place. In this article, we will learn different ways to insert elements into a vector in C++ and also compare their efficiency.The simplest way to 3 min read Like