cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : stack
- -
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 class template
<stack>

LIFO stack

Stacks are a type of container adaptors, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from the end of the container.

stacks are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed/popped from the "back" of the specific container, which is known as the top of the stack.

The underlying container may be any of the standard container class templates or some other specifically designed container class. The only requirement is that it supports the following operations:

  • front()
  • back()
  • push_back()
  • pop_back()

Therefore, the standard container class templates vector, deque and list can be used. By default, if no container class is specified for a particular queue class, the standard container class template deque is used.

In their implementation in the C++ Standard Template Library, stacks take two template parameters:

template < class T, class Container = deque<T> > class stack;
Where the template parameters have the following meanings:
  • T: Type of the elements.
  • Container: Type of the underlying container object used to store and access the elements.
In the reference for the stack member functions, these same names are assumed for the template parameters.

Member functions

(constructor) Construct stack (public member function)
empty Test whether container is empty (public member function)
size Return size (public member function)
top Access next element (public member function)
push Add element (public member function)
pop Remove element (public member function)

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