Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++
communitycreator
stack

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

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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();
}
}

RELATED TAGS

c++
communitycreator
stack

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring