Export (0) Print
Expand All

stack Class

A template container adaptor class that provides a restriction of functionality limiting access to the element most recently added to some underlying container type. The stack class is used when it is important to be clear that only stack operations are being performed on the container.

template < 
   class Type,  
   class Container=deque<Type>  
> 
class stack

Type

The element data type to be stored in the stack.

Container

The type of the underlying container used to implement the stack. The default value is the class deque<Type>.

The elements of class Type stipulated in the first template parameter of a stack object are synonymous with value_type and must match the type of element in the underlying container class Container stipulated by the second template parameter. The Type must be assignable, so that it is possible to copy objects of that type and to assign values to variables of that type.

Suitable underlying container classes for stack include deque, list, and vector, or any other sequence container that supports the operations of back, push_back, and pop_back. The underlying container class is encapsulated within the container adaptor, which exposes only the limited set of the sequence container member functions as a public interface.

The stack objects are equality comparable if and only if the elements of class Type are equality comparable and are less-than comparable if and only if the elements of class Type are less-than comparable.

  • The stack class supports a last-in, first-out (LIFO) data structure. A good analogue to keep in mind would be a stack of plates. Elements (plates) may be inserted, inspected, or removed only from the top of the stack, which is the last element at the end of the base container. The restriction to accessing only the top element is the reason for using the stack class.

  • The queue class supports a first-in, first-out (FIFO) data structure. A good analogue to keep in mind would be people lining up for a bank teller. Elements (people) may be added to the back of the line and are removed from the front of the line. Both the front and the back of a line may be inspected. The restriction to accessing only the front and back elements in this way is the reason fur using the queue class.

  • The priority_queue class orders its elements so that the largest element is always at the top position. It supports insertion of an element and the inspection and removal of the top element. A good analogue to keep in mind would be people lining up where they are arranged by age, height, or some other criterion.

stack

Constructs a stack that is empty or that is a copy of a base container object.

container_type

A type that provides the base container to be adapted by a stack.

size_type

An unsigned integer type that can represent the number of elements in a stack.

value_type

A type that represents the type of object stored as an element in a stack.

empty

Tests if the stack is empty.

pop

Removes the element from the top of the stack.

push

Adds an element to the top of the stack.

size

Returns the number of elements in the stack.

top

Returns a reference to an element at the top of the stack.

Header: <stack>

Namespace: std

Show:
© 2014 Microsoft