TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ Stack pop() Function

C++ Stack pop() Function with Examples on tutorial for beginners and professionals, constructor, emplace(), empty(), pop(), push(), size(), top(), object and class, exception, static, structs, inheritance, aggregation etc.

<< Back to CPP

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

void pop()

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.

#include <iostream>
#include <stack>
int main()
{
	std::stack<int> newstack; 
	for(int j=0; j<5; j++)
	newstack.push(j);
	std::cout <<"Popping out elements?";
	while (!newstack.empty () )
	{
		std::cout <<" " << newstack.top();
		newstack.pop();
	}
	std::cout<<"\n";
	return 0;
}

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.

#include <iostream>
#include <stack>
int main()
{
	std::stack<int> newstack; 
	newstack.push(11);
	newstack.push(22);
	newstack.push(33);
	newstack.push(44);
	std::cout << "Popping out elements?";
	newstack.pop();
	newstack.pop();
	while (!newstack.empty () )
	{
		std::cout << " "<< newstack.top();
		newstack.pop();
	}
	std::cout<<"\n";
	return 0;
}

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.

#include <iostream>
#include <stack>
int main()
{
                      std::stack<int> newstack;
		newstack.push(69);
		newstack.push(79);
		newstack.push(80);
		newstack.push(85);
		newstack.push(90);
		while (!newstack.empty () )
		{
			std::cout<< " " << newstack.top ();
			newstack.pop();
		}
		return 0;
}

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




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf