Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ Stack::emplace() Function



The C++ std::stack::emplace() function constructs and inserts a new element at the top of the stack. The new element is inserted in place i.e. without performing copy or move operation. This function accepts a single value as an argument which is forwarded to construct the new element in the stack.

The member function emplace() allows constructing an object in place in the container (stack), while the underlying container's member function emplace_back() is responsible for actually adding the object to the container. Hence, the emplace() function effectively calls the emplace_back() function with the forwarded arguments.

The emplace() function is more efficient than the push() function because it avoids the overhead (use of extra memory) of creating a temporary object and copying or moving it into the stack.

Syntax

Following is the syntax for std::stack::emplace() function −

void stack_name.emplace(value);

Parameters

value − It is the value forwarded to construct the new elements.

Return value

This function does not return any value.

Example 1

The following example shows the usage of the std::stack::emplace() function. First, we are trying to create a stack s. Then, use a for loop to insert five elements into the stack using emplace() function. Each element is created by adding the value "1" with the current value of i. Finally, we use the pop() function to pop all the elements from the stack and print them to the console.

#include <iostream>
#include <stack>
using namespace std;

int main(void) {
   stack<int> s;
   for (int i = 0; i < 5; ++i)
      s.emplace(i + 1);
   while (!s.empty()) {
      cout << s.top() << endl;
      s.pop();
   }
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

5
4
3
2
1

Example 2

Here, we are using the emplace() function to insert three simple strings at the top of the stack and print them.

#include <iostream>
#include <stack>
#include <string>

int main() {
   std::stack<std::string> myStack;
   // Use emplace() to insert elements into the stack
   myStack.emplace("first");
   myStack.emplace("second");
   myStack.emplace("third");
   // Print the elements of the stack
   std::cout << "Elements in stack are: ";
   while (!myStack.empty()) {
      std::cout << myStack.top() << std::endl;
      myStack.pop();
   }
   return 0;
}

Output

If we run the above code it will generate the following output −

Elements in stack are: third
second
first

Example 3

Now, we are trying to insert the table of 10 at the top of the stack and then respectively printing it along with the total count of elements present in the stack −

#include <iostream>
#include <stack>
using namespace std;

int main(){
   stack<int> stck;
   int total = 0;
   stck.emplace(10);
   stck.emplace(20);
   stck.emplace(30);
   stck.emplace(40);
   stck.emplace(50);
   stck.emplace(60);
   stck.emplace(70);
   stck.emplace(80);
   stck.emplace(90);
   stck.emplace(100);
   cout << "Elements in stack are: ";
   while (!stck.empty()){
      cout<<stck.top() << " ";
      stck.pop();
      total++;
   }
   cout<<"\nTotal number of elements in stack are: "<<total;
   return 0;
}

Output

Output of the above code is as follows −

Elements in stack are: 100 90 80 70 60 50 40 30 20 10 
Total number of elements in stack are: 10

Example 4

The emplace() function is more efficient than the push() function because it avoids the overhead (use of extra memory) of creating a temporary object and copying or moving it into the stack.

In this example, we define a custom class MyClass with a constructor and copy/move constructors that print messages to the console. We then create an empty stack of MyClass objects and insert three elements into the stack using both emplace() function and the push() function.

When using emplace() function, we provide each element as an argument to the function, and we see that each element is created in place and inserted into the stack.

When using the push() function, we create temporary objects of type MyClass and then copy or move them into the stack. This requires additional memory to store the temporary objects and can result in additional overhead for copying or moving the objects into the stack.

#include <iostream>
#include <stack>
#include <string>

class MyClass {
   public:
      MyClass(const std::string& str) : mStr(str) {
         std::cout << "Constructing MyClass with string: " << mStr << std::endl;
      }
      MyClass(const MyClass& other) : mStr(other.mStr) {
         std::cout << "Copying MyClass with string: " << mStr << std::endl;
      }
      MyClass(MyClass&& other) : mStr(std::move(other.mStr)) {
         std::cout << "Moving MyClass with string: " << mStr << std::endl;
      }
   private:
      std::string mStr;
};
int main() {
   std::stack<MyClass> myStack;
   std::cout << "Using emplace()" << std::endl;
   myStack.emplace("first");
   myStack.emplace("second");
   myStack.emplace("third");
   std::cout << "Using push()" << std::endl;
   MyClass myClass1("first");
   MyClass myClass2("second");
   MyClass myClass3("third");
   myStack.push(myClass1);
   myStack.push(myClass2);
   myStack.push(myClass3);
   return 0;
}

Output

We get the following output while executing the above code −

Using emplace()
Constructing MyClass with string: first
Constructing MyClass with string: second
Constructing MyClass with string: third
Using push()
Constructing MyClass with string: first
Constructing MyClass with string: second
Constructing MyClass with string: third
Copying MyClass with string: first
Copying MyClass with string: second
Copying MyClass with string: third
Advertisements