What is stack.push() in C++?

The stack.push() function in C++ inserts an element at the top of the stack.

The following illustration shows a visual representation of the stack.push() function.

A visual representation of the stack.push() function

In order to use the stack.push() function, we must include stack in the program as shown below:

#include <stack>

Syntax

stack_name.push(element)
// where the stack_name is the name of the stack

Parameter

This function requires an element as a parameter.

Return value

None.

Code

#include <iostream>
//header file
#include <stack>
using namespace std;
int main() {
stack<int> stack1;
stack1.push(2);
stack1.push(5);
stack1.push(3);
stack1.push(1);
stack1.push(0);
//stack1 = 0->1->3->5->2
cout<<"The stack1 after series of push : \n";
while(stack1.empty()==false)
{
cout << stack1.top() << " ";
stack1.pop();
}
}

Free Resources