stack::emplace() method

In C++, the standard library gives us the facility to use stacks as a type of container adaptors with LIFO type of working. The stack::emplace() is a C++ Standard Library method which is used to construct and insert an element into the stack.

Syntax:

stack_name.emplace(val);

Parameters: The stack::emplace() method accepts a single parameter:
•Argument to construct a new element
Return value: none

Example of stack::emplace() method

#include <iostream>
#include <stack>
using namespace std;
int main ()
{
  stack<int> s;
  for(int i = 1; i <= 5; i++)
    s.emplace(i);
  while (!s.empty())
  {
    cout<<s.top()<<"  ";
    s.pop();
  }
  return 0;
}

Output:

5  4  3  2  1

This article is written by

Please comment below, if you have any doubts or find any error in the above article.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top