Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++
stack
pop
communitycreator

What is stack.pop() 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.pop() function in C++ removes the element that is available at the top of the stack.

Figure 1 shows the visual representation of the stack.pop() function.

Figure 1: Visual representation of stack.pop() function

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

#include <stack>

Syntax

stack_name.pop()
// where the stack_name is the name of the stack

Parameter

This function does not require a parameter.

Return value

This function returns the element that is available at the top of the stack and removes that element from the stack.

Example

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

RELATED TAGS

c++
stack
pop
communitycreator

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