cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : stack : push
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
stack
comparison operators
stack::stack
member functions:
· stack::empty
· stack::pop
· stack::push
· stack::size
· stack::top

-

stack::push public member function
void push ( const T& x );

Add element

Adds 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 x.

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

Parameters

x
Value to be copied to the new element.
T is the first template parameter (the type of the elements stored in the stack).

Return value

none

Example

// stack::push/pop
#include <iostream>
#include <stack>
using namespace std;

int main ()
{
  stack<int> mystack;

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

  cout << "Popping out elements...";
  while (!mystack.empty())
  {
     cout << " " << mystack.top();
     mystack.pop();
  }
  cout << endl;

  return 0;
}

Output:

Popping out elements... 4 3 2 1 0


Complexity

Constant.

See also

stack::pop Remove element (public member function)
stack::size Return size (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us