Javatpoint Logo
Javatpoint Logo

C++ Stack pop() Function

C++ Stack pop() function is used for removing the topmost element of the stack. This function performs the deletion operation. Deletion in a stack is done from the top. The element which was most recently inserted is deleted first.The stack follows the LIFO principle which is Last In First Out and hence the pop operation follows the order as mentioned above.

Syntax

Parameters

The function takes no parameter and is used only for the deletion of the top element. Also since the stack follows LIFO principle we do not need to specify which element is to be deleted as it is by default understood that the top most element will be removed first.

Return value

The function is used only for the removal of elements from the stack and has no return value. Hence we can say that the return type of the function is void.

Example 1

//The program is used to demonstrate the use of the pop() function of the stack by insertion of simple integer values.

Output:

Popping out elements... 4 3 2 1 0

Example 2

//The program is used to demonstrate the use of the pop() function of the stack by insertion of simple integer values.

Output:

Popping out elements... 22 11

Example 3

//The program is used to demonstrate the use of the pop() function of the stack by insertion of simple integer values.

Output:

90 85 80 79 69

Complexity

The complexity of the function is constant, the function only performs the pop or delete operation on the top of the stack and does not make any add on the complexity.

Data races

The modification is made to the container and the elements contained. By the deletion operation change is reflected at the element in the top position, the top position is shifted to one unit downwards. It can be demonstrated as top=top--.

Exception Safety

Guarantee as equivalent to the operations that are performed on the underlying container object is provided.

Next TopicC++ Stack




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials