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. SyntaxParametersargs: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. 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. 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. 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 |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India