English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++ The push() function of the Stack is used to add a new element at the top of the stack. If we have an array of type stack and we can insert a new element into the stack by using the push() function. The element will be inserted at the top of the stack. As the stack follows the LIFO principle, the first element inserted will be deleted at the end, and vice versa, because the stack follows the Last In First Out principle.
void push(const value_type& value);
value:This parameter indicates the value initialized for the element. This parameter specifies the value of the new element inserted. After the function execution, the element "val" becomes the new top element of the stack.
This function only inserts elements and does not return any value. The return type of this function can be considered invalid.
//This program demonstrates the usage of the push() function of the stack by inserting simple integer values.
#include <iostream> #include <stack> using namespace std; int main() { stack<int> newstack; for(int j = 0; j<5; j++) newstack.push(j); cout << "Pop elements from the stack......"; while (!newstack.empty()) { cout << " " << newstack.top(); newstack.pop(); } cout << '\n'; return 0; }
Output:
Pop elements from the stack..... 4 3 2 1 0
#include <iostream> #include <stack> using namespace std; int main() { stack<int> newstack; newstack.push(69); newstack.push(79); newstack.push(80); while (!newstack.empty()) { cout << " " << newstack.top(); newstack.pop(); } return 0; }
Output:
90 85 80 79 69
//This program demonstrates the usage of the push() function of the stack by inserting simple integer values.
#include <iostream> #include <stack> using namespace std; int main() { stack<int> newstack; newstack.push(11); newstack.push(22); newstack.push(33); newstack.push(44); cout << "Pop element..."; newstack.pop(); newstack.pop(); while (!newstack.empty()) { cout << " " << newstack.top(); newstack.pop(); } cout << '\n'; return 0; }
Output:
Pop element... 22 11
//This program demonstrates the usage of the push() function of the stack by inserting simple integer values.
#include <iostream> #include <stack> using namespace std; int main() { stack<int> a,b; a.push(5); a.push(8); a.push(50); b.push(132); b.push(45); cout << " Size of a: " << a.size(); cout << "\n Size of b: " << b.size(); return 0; }
Output:
Size of a: 3 Size of b:2
A call that pushes back on the underlying container is necessary to complete the insertion operation on the element.
Modifies the container and the elements it contains. Adding a new element modifies all underlying stack elements.
Guarantees equivalent operations to those performed on the underlying container objects.