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