This page lists the STL container and container adaptor class types.
These are all template classes that provide containers for holding and
manipulating elements of any user-chosen or user-defined type.
The term first-class container, when applied to a container
type, simply means that the container class has all the characteristics
and behaviors that a user would expect of any data type, given the
user's previous experience (with built-in data types, for example). The
first-class containers include the vector, deque,
list, map, multimap, set, and
multiset classes.
The term container adaptor, on the other hand refers to an
"adaptation" of one of the first-class containers by modifying and
restricting its interface for some special purpose. These container
adaptors include the stack and queue, which are
based (by default) on the deque, as well as the
priority_queue, which is based (by default) on the
vector.
First-class containers
Sequential containers
- vector
- Provides linear, contiguous storage, with fast inserts at the
"back" end only. This class is often referred to as a "better array"
that we should all now use in place of arrays. Like a great deal of
free advice, this too should be followed with caution.
- deque
- Provides linear, non-contiguous storage, with fast inserts at
both ends.
- list
- Provides linear, doubly-linked storage, with fast inserts
anywhere.
Associative containers
- map
- Provides a collection of 1-to-1 mappings, i.e. a collection of
key/value "pairs" (pair objects) in which the first element
of such a pair is a key and the second element of the
pair is the value corresponding to that key, and the
pair objects are maintained in sorted key order. In a map
the keys must be unique.
- multimap
- Like a map, except that pairs with duplicate keys are
permitted.
- set
- Provides a set of items which must have keys (or themselves be
keys, in the simplest cases), with fast associative lookup. In a set
the item keys must be unique, but (somewhat counter intuitively for a
set) the item keys are ordered.
- multiset
- Like a set, except that items with duplicate keys are
permitted.
Container adaptors
Each of these "container adaptors" is constructed by taking one of
the sequential containers given above and "adapting"
(restricting/modifying) its interface to provide the desired
behavior.
- stack
- Adapts the deque container to provide strict last-in, first-out
(LIFO) behavior.
- queue
- Adapts the deque container to provide strict first-in, first-out
(FIFO) behavior.
- priority_queue
- Adapts the vector container to maintain items in a sorted
order.
Other (non-STL) classes with STL-like characteristics and
behaviors
There are some other classes that are regarded as near-containers, in
that (for example) they can be used with iterators, which makes them
accessible to manipulation via the STL algorithms.
- string
- Not to be confused with the legacy C-string data type, inherited
from the C-language, and consisting of a simple character array with
the characters of interest terminated with a null character. The C++
string class is indeed a bona fide class, with much more
flexibility (and safety) than the older C-string, and should probably
be used anywhere the C-string is not explicitly required. (But that's
more free advice.)
- bitset
- Provides a model for a fixed-size array of bits or boolean
values, which is used to manage a set of "on-off switches" (or
"flags") and where the state of a bitset object at any time
will represent some combination of such flags (or on-off
switches).
- valarray
- Provides a representation for the mathematical concept of a
linear sequence of values and is designed to permit the efficient
performance of certain numerical calculations. That having been said,
some writers argue that the valarray class is poorly
designed and that there are better alternatives if you wish to do
serious numerical calculations in C++.
Note that for many purposes even "ordinary" C-style arrays (which
are not classes) and C-strings (which are also not classes) can be
accessed from the STL with C-style pointers in the same way that STL
containers are accessed with STL iterators.