English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++The stack emplace() function adds a new element at the top of the stack above the current top element. Now, we have a stack with an existing element, and we want to insert or push a new element into the stack. For this purpose, we use this function.
template<class... Args> void emplace(Args&&... args);
args: The parameter forwarding is used for the parameters of constructing the new element. That is, the element specified by args will be inserted above the current top element in the stack. Now, the newly inserted element becomes the top element, and all push and pop operations are performed on it.
This function is only used to add new elements and does not return any value. Therefore, the return type of this function is void.
//This program demonstrates the usage of the emplace function by adding two simple strings at the top of the stack and printing them.
#include<iostream> #include<stack> #include<string> using namespace std; int main() { stack<string> newstack; newstack.emplace("I am the first one"); newstack.emplace("I am the second one"); cout << "newstack's content: \n" while (!newstack.empty()) { cout << newstack.top() << '\n'; newstack.pop(); } return 0; }
Output:
newstack's content: I am the second one I am the first one
//This program adds11Insert the table of, and then print them separately to demonstrate the usage of the emplace function.
#include<iostream> #include<stack> #include<string> using namespace std; int main() { stack<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"); cout << "newstack's content: \n" cout << "Table of 11"; while (!newstack.empty()) { cout << newstack.top() << '\n'; newstack.pop(); } return 0; }
Output:
newstack's content: Table of 11121 99 88 77 66 55 44 33 22 11
//This program demonstrates the usage of the emplace function by adding two simple strings at the top of the stack and printing them.
#include<iostream> #include<stack> #include<string> using namespace std; int main() { stack<string> newstack; newstack.emplace("We can see the application of the emplace function in the stack here"); newstack.emplace("The new element added by the function is at the top of the stack"); while (!newstack.empty()) { cout << newstack.top() << '\n'; newstack.pop(); } return 0; }
Output:
The new element added by the function is at the top of the stack We can see the application of the emplace function in the stack here
A call to emplace_back was made. This function is used to insert a new element, which is done by making a single call.
All elements existing in the stack are modified. Since the element is added to the top, the corresponding positions of all other elements also change.
It provides guarantees equivalent to the operations performed on the underlying container objects.