public member function
<stack>

std::stack::emplace

template <class... Args> void emplace (Args&&... args);
Construct and insert element
Adds a new element at the top of the stack, above its current top element. This new element is constructed in place passing args as the arguments for its constructor.

This member function effectively calls the member function emplace_back of the underlying container, forwarding args.

Parameters

args
Arguments forwarded to construct the new element.

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// stack::emplace
#include <iostream>       // std::cin, std::cout
#include <stack>          // std::stack
#include <string>         // std::string, std::getline(string)

int main ()
{
  std::stack<std::string> mystack;

  mystack.emplace ("First sentence");
  mystack.emplace ("Second sentence");

  std::cout << "mystack contains:\n";
  while (!mystack.empty())
  {
    std::cout << mystack.top() << '\n';
    mystack.pop();
  }

  return 0;
}

Output:

mystack contains:
Second sentence
First sentence


Complexity

One call to emplace_back on the underlying container.

Data races

The container and up to all its contained elements are modified.

Exception safety

Provides the same level of guarantees as the operation performed on the underlying container object.

See also