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

Stack Emplace Function

BLUF: Mastering Stack Emplace 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 Emplace Function

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

The emplace function in C++ Stack inserts a new element above the current top element. When adding a new element to a stack with existing elements, this function is utilized for inserting or pushing the element onto the stack.

Syntax

Example

template <class... Args> void emplace (Args&&... args);

Parameters

The argument is passed to create a new element, which is then added on top of the current element in the stack. This newly added element becomes the top element, and all subsequent push and pop actions are executed on this element.

Return value

The function serves the sole purpose of adding new elements and does not yield any output. Therefore, the function's return type is void.

Example 1

//The code snippet demonstrates the application of the emplace function by inserting two basic strings at the stack's beginning and displaying them.

Example

#include<iostream>
#include<stack>
#include<string>
int main()
{
	std: : stack<std: : string> newstack;
	newstack.emplace (?I am the first line?);
	newstack.emplace (?I am the second one?);
	std: :cout << ?Contents of newstack: \n?;
	while (!newstack.empty () )
	{
		std: :cout << newstack.top () << ?\n?;
		newstack.pop ();
	}
	return 0;
}

Output:

Output

Contents of newstack:
I am the second one
I am the first line

Example 2

//The following program demonstrates the utilization of the emplace function by inserting the multiplication table of 11 and subsequently displaying it.

Example

#include<iostream>
#include<stack>
#include<string>
int main()
{
	std: : stack<std: : string> newstack;
	newstack.emplace (?11?);
	newstack.emplace (?22?);
	newstack.emplace (?33?);
	newstack.emplace (?44?);
	newstack.emplace (?55?);
	newstack.emplace (?66?);
	newstack.emplace (?77?);
	newstack.emplace (?88?);
	newstack.emplace (?99?);
	newstack.emplace (?121?);
	std: :cout << ?Contents of newstack: \n?;
	 std: :cout <<?Table of 11?;

	while (!newstack.empty () )
	{
		std: :cout << newstack.top () << ?\n?;
		newstack.pop ();
	}
	return 0;
}

Output:

Output

Contents of newstack: 
Table of 11121
99
88
77
66
55
44
33
22
11

Example 3

//The following code snippet demonstrates the application of the emplace function by inserting two basic strings at the top of the stack and then displaying them.

Example

#include<iostream>
#include<stack>
#include<string>
int main()
{
	std::stack<std::string> newstack;
	newstack.emplace ("We are here to see the application use of emplace function in stacks");
	newstack.emplace ("The function adds new elements are the top of the stack");
	while (!newstack.empty () )
	{
		std::cout << newstack.top () << "\n";
		newstack.pop ();
	}
	return 0;
}

Output:

Output

The function adds new elements are the top of the stack         
We are here to see the application use of emplace function in stacks

Complexity

One invocation is performed on the emplace_back function. This function is utilized to add a fresh element by executing a single call.

Data races

All elements within the stack undergo modification. As the new element is inserted at the top, it causes a shift in the positions of all other elements accordingly.

Exception Safety

A assurance is given that matches the operations carried out on the original container object.

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