Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only.
This function is used to insert a new element into the stack container, the new element is added on top of the stack.
Syntax :
stackname.emplace(value) Parameters : The element to be inserted into the stack is passed as the parameter. Result : The parameter is added to the stack at the top position.
Examples:
Input : mystack{1, 2, 3, 4, 5};
mystack.emplace(6);
Output : mystack = 6, 5, 4, 3, 2, 1
Input : mystack{};
mystack.emplace(4);
Output : mysstack = 4
Note: In stack container, the elements are printed in reverse order because the top is printed first then moving on to other elements.
Errors and Exceptions
1. It has a strong exception guarantee, therefore, no changes are made if an exception is thrown.
2. Parameter should be of same type as that of the container, otherwise an error is thrown.
// CPP program to illustrate // Implementation of emplace() function #include <iostream> #include <stack> using namespace std; int main() { stack<int> mystack; mystack.emplace(1); mystack.emplace(2); mystack.emplace(3); mystack.emplace(4); mystack.emplace(5); mystack.emplace(6); // stack becomes 1, 2, 3, 4, 5, 6 // printing the stack cout << "mystack = "; while (!mystack.empty()) { cout << mystack.top() << " "; mystack.pop(); } return 0; } |
Output:
6 5 4 3 2 1
Time Complexity : O(1)
Difference between stack::emplace() and stack::push() function.
While push() function inserts a copy of the value or the parameter passed to the function into the container at the top, the emplace() function constructs a new element as the value of the parameter and then adds it to the top of the container.
Application :
Given a number of integers, add them to the stack using emplace() and find the size of the stack without using size function.
Input : 5, 13, 0, 9, 4 Output: 5
Algorithm
1. Insert the given elements to the stack container using emplace() one by one.
2. Keep popping the elements of stack until it becomes empty, and increment the counter variable.
3. Print the counter variable.
// CPP program to illustrate // Application of emplace() function #include <iostream> #include <stack> using namespace std; int main() { int c = 0; // Empty stack stack<int> mystack; mystack.emplace(5); mystack.emplace(13); mystack.emplace(0); mystack.emplace(9); mystack.emplace(4); // stack becomes 5, 13, 0, 9, 4 // Counting number of elements in queue while (!mystack.empty()) { mystack.pop(); c++; } cout << c; } |
Output:
5
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- priority_queue emplace() in C++ STL
- set::emplace() in C++ STL
- multiset::emplace() in C++ STL
- emplace vs insert in C++ STL
- map emplace() in C++ STL
- vector emplace() function in C++ STL
- list emplace() function in C++ STL
- unordered_set emplace() function in C++ STL
- unordered_multiset emplace() function in C++ STL
- unordered_multimap emplace() function in C++ STL
- deque emplace in C++ STL
- unordered_map emplace() in C++ STL
- queue::emplace() in C++ STL
- multimap::emplace() in C++ STL
- stack empty() and stack size() in C++ STL
- Inserting elements in std::map (insert, emplace and operator [])
- Sort a stack using a temporary stack
- Stack in C++ STL
- stack swap() in C++ STL
- stack top() in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

