C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Stack emplace() FunctionC++ Stack emplace() function adds a new element at the top of the stack above the current top element. Now, we have a stack with already existing elements, and we wish to insert or push a new element in the stack, for doing that we use this function. Syntaxtemplate <class... Args> void emplace (Args&&... args); Parametersargs:The parameter forwards the argument for the construction of new element. That is the element specified by args is inserted in the stack over the current top element. The newly inserted element now becomes the top element and all the push and pop operations are performed on it. Return valueThe function is used only for the addition of new elements and does not return any value. Hence the return type of the function is void. Example 1//The program illustrates the use of emplace function by adding two simple strings at the top of the stack and printing 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 program illustrates the use of emplace function by inserting the table of 11 on the and then respectively printing 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 program illustrates the use of emplace function by adding two simple strings at the top of the stack and printing 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 ComplexityOne call is made to the emplace_back. The function is used for inserting a new element which is done by making a single call. Data racesAll the elements present in the stack are modified. Since the element is added to the top hence respective positions of all the other elements are also changed. Exception SafetyGuarantee as equivalent to the operations that are performed on the underlying container object is provided.
Next TopicC++ Stack
|