cppreference.com -> C++ Stacks -> Details

C++ Stacks


Operators

Syntax:

  ==
  <=
  >=
  <
  >
  !=

All of the above operators can be used on stacks. Equality is defined by stacks having the same elements in the same order.


empty

Syntax:

  bool empty();

The empty() function returns true if the current stack is empty, and false otherwise.


pop

Syntax:

  void pop();

The function pop() removes the top element of the current stack.

Related topics:
top(),

push

Syntax:

  void push( const TYPE &val );

The function push() adds val to the top of the current stack. For example:

    stack<int> s;
    for( int i=0; i < 10; i++ )
      s.push(i);
    

size

Syntax:

  size_type size();

The size() function returns the number of elements in the current stack. For example:

    stack<int> s;
    for( int i=0; i < 10; i++ )
      s.push(i);
    cout << "This stack has a size of " << s.size() << endl;
    
    

top

Syntax:

   TYPE &top();

The function top() returns a reference to the top element of the stack. For example, the following code displays and empties a stack.

    while( !s.empty() ) {
      cout << s.top() << " ";
      s.pop();
    }
Related topics:
pop(),