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.
In order to use the stack.pop()
function, we must include stack
in the program, as shown below:
#include <stack>
stack_name.pop()
// where the stack_name is the name of the stack
This function does not require a parameter.
This function returns the element
that is available at the top of the stack and removes that element
from the stack.
#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->0cout<<"The length of stack before pop: \n"<<stack1.size()<<"\n";stack1.pop();//after the pop stack1 = 3->5->2->0cout<<"The length of stack after pop: \n"<<stack1.size();}