public member function
<stack>

std::stack::push

void push (const value_type& val);
void push (const value_type& val);void push (value_type&& val);
Insert element
Inserts a new element at the top of the stack, above its current top element. The content of this new element is initialized to a copy of val.

This member function effectively calls the member function push_back of the underlying container object.

Parameters

val
Value to which the inserted element is initialized.
Member type value_type is the type of the elements in the container (defined as an alias of the first class template parameter, T).

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// stack::push/pop
#include <iostream>       // std::cout
#include <stack>          // std::stack

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

  for (int i=0; i<5; ++i) mystack.push(i);

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

  return 0;
}

Output:
Popping out elements... 4 3 2 1 0


Complexity

One call to push_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