Stack Push Function - C++ Programming Tutorial
C++ Course / Data Structures / Stack Push Function

Stack Push Function

BLUF: Mastering Stack Push Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Stack Push Function

C++ is renowned for its efficiency. Learn how Stack Push Function enables low-level control and high-performance computing in the tutorial below.

C++ Stack push method is employed to append fresh elements to the stack's top. In case of an array of stack type, the push function facilitates the addition of new elements. These new elements are placed at the stack's highest position. The element initially inserted will be the first to be removed, adhering to the Last In First Out (LIFO) principle that stacks follow.

Syntax

Example

void push (const value_type& value);

Parameters

The value parameter indicates the initial value of the element. It defines the value of the newly added element, setting 'val' as the stack's new top element post-execution.

Return value

The function solely adds an element without returning any data. The function's return type can be considered as void.

Example 1

//The script showcases the utilization of the push method in a stack by adding basic integer values.

Example

#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:

Output

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

Example 2

Example

#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:

Output

90 85 80 79 69

Example 3

//This code showcases how to utilize the push method of the stack to insert basic integer values.

Example

#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:

Output

Popping out elements... 22 11

Example 4

//The script showcases how to utilize the push method of a stack through adding basic integer values.

Example

#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:

Output

Size of a: 3
Size of b:2

Complexity

One invocation is performed on the push back function of the underlying container, which is essential for finalizing the insertion process of the element.

Data races

The adjustment is applied to the container and its encompassed elements. Adding a new element alters all the stack elements underneath.

Exception Safety

A promise is given that matches the actions executed on the container object underneath.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience