The STL stack is a container adaptor. That is, it is not a "first-class" container, but instead simply "adapts" one of the sequential first-class containers (by default, the deque) for its own purposes. So, the deque interface is restricted (i.e., much of it is hidden) so that the required LIFO (Last In, First Out) stack-like behavior is provided.

Constructors and destructor

Default Constructor

stack<T> s;
Construct an empty stack s which can hold values of type T.

Copy Constructor

There is, of course, only one copy constructor, but there are two syntactic forms that invoke it, and both are shown.

stack<T> s(otherStack);
Construct s as a copy of otherStack, whose component type must be T.
stack<T> s = otherStack;
Copy constructor (alternate usage syntax).

Destructor

Any stack will have a container data member (by default, a deque) which will hold its elements. That data member will have its own destructor which will automatically be invoked when the stack goes out of scope.

Overloaded operators

Assignment operator

s1 = s2
Assign s2 to s1, and return the common value. The stack on the left of an assignment receives the values and size of the one on the right.

Relational operators

Stacks are compared in the "lexicographic ordering" sense. This essentially means that the two stacks are compared by comparing their values pairwise, starting at the beginning, with each comparison looking at two values in corresponding positions, until a determination of the relationship between the two stacks can be made. Only stacks of the same component type can be compared of course, and the == and < operators must be defined for the component type.

s1 == s2
Return true if s1 and s2 have the same component type and the same size, and the components in each pair of corresponding locations have the same value; otherwise return false.
s1 != s2
Return true if s1==s2 returns false; otherwise return false.
s1 < s2
Return true if, in the pairwise comparison of values from s1 and s2, in the first pair in which the two values differ, the one from s1 is less than the one from s2; otherwise return false.
s1 <= s2
Return true if either s1<s2 or s1==s2 is true; otherwise return false.
s1 > s2
Return true if s2<s1 is true; otherwise return false.
s1 >= s2
Return true if either s1>s2 or s1==s2 is true, otherwise return false.

Member function for accessing a value

s.top()
Return a reference (or const_reference) to the top component of s.

Member functions for reporting status

s.size()
Return a value of type size_type giving the number of values currently in s.
s.empty()
Return true if s is empty (contains zero values); otherwise return false.

Member function for inserting a value

s.push(val)
Add val to the top of s, increasing the size of s by one.

Member function for deleting a value

s.pop()
Delete the top value of s, decreasing the size of s is by one.

Miscellaneous notes

Implementation
Standard C++ does not say how the STL containers and algorithms must be implemented. It does, however, state certain constraints, such as complexity constraints, to which each implementation must adhere. Thus it is much better to base your programs on the STL's performance guarantees, rather than upon any assumption about how a particular feature, like the stack class, may be implemented. The stack container adaptor is based by default on the deque. Either a vector or a list could also be used, since all three provide the push_back(), pop_back() and back() operations that are necessary to support the stack interface. An argument for the choice of deque as the default over vector (for example) could be based on the fact that deques, unlike vectors, free their memory when components are deleted, and neither do they have to copy all components when reallocating.

Sample programs

All programs have been compiled and run successfully under Microsoft Visual Studio .NET 2005, unless otherwise noted.

stack01.cpp | Windows_executable | program_output (text)
Illustrates the "LIFO" (Last In, First Out) behavior of a simple stack of characters, as well as its default constructor, its copy constructor, and the stack push(), pop(), top(), empty(), and size() member functions.
stack02.cpp | Windows_executable | program_output (text)
Illustrates the creation of a stack using values from a deque (when the underlying container is the default one, namely a deque), and from a vector, when the underlying container is a vector.
stack03.cpp | Windows_executable | program_output (text)
Illustrates the assignment of one stack to another, and the comparison of stacks.
stack04.cpp | Windows_executable | program_output (text)
Illustrates how *not* to access the components of a stack.

Member function prototypes

Template specification for the stack class

template<class T,
         class Allocator = allocator<T> >
class stack { ... }

Default Constructor

explicit stack(const Container& c = Container());

Copy Constructor

stack(const Container& otherContainer);

Destructor

~stack();

empty

bool empty() const;

pop

void pop();

push

void push(const T& val);

size

size_type size() const;

top

      T& top() const;
const T& top() const;