TheDeveloperBlog.com

Home | Contact Us

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

C++ Stack push() Function

C++ Stack push() 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 push() Function

C++ Stack push () function is used for adding new elements at the top of the stack. If we have an array of type stack and by using the push() function we can insert new elements in the stack. The elements are inserted at the top of the stack. The element which is inserted most initially is deleted at the end and vice versa as stacks follow LIFO principle.

Syntax

void push (const value_type& value);

Parameters

value: The parameter represents the value to which the element is initialized. The parameter specifies the value of the newly inserted element. The element 'val' becomes the new top element of the stack after function execution.

Return value

The function only inserts element and does not return any value. The return type of the function can be thought as void.

Example 1

//The program is used to demonstrate the use of the push() 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 << "Poping the elements out of the stack??.";
         while (!newstack.empty () )
         {
	   std::cout<<" " << newstack.top ();
	    newstack.pop();
	}
	

std::cout<<"\n";
return 0;
}

Output:

Poping the elements out of the stack..... 4 3 2 1 0

Example 2

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

Output:

90 85 80 79 69

Example 3

//The program is used to demonstrate the use of the push() 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 4

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

#include <iostream>
#include <stack>
int main()
{
	std::stack<int> a,b;
	a.push(5); a.push(8); a.push(50);
	b.push(132); b.push(45);
	std::cout<<"Size of a: "<<a.size();
	std::cout<<"\n Size of b:" <<b.size();
	return 0;
}

Output:

Size of a: 3
Size of b:2 

Complexity

One call is made to the push back on the container that is underlying, which is necessary for the insertion operation on the element to get completed.

Data races

The modification is made to the container, and the elements contained. The addition of a new element modifies all the underlying stack elements.

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