The STL makes very good use of the C++ typedef mechanism
for giving the same name to the same conceptual entity across different
container classes. The best example is the use of the term
iterator, a typedef alias for whatever default kind
of iterator a given class provides. For example,
vector<int>::iterator gives a random access iterator for
a vector of integers, while list<string>::iterator gives
a bidirectional iterator for a list of strings.
typedef name |
Does the typedef name appear in the class
(of the container or container adaptor)? |
first-class containers |
container adaptors |
vector
deque
list |
map
multimap |
set
multiset |
stack
queue
priority_queue |
size_type |
Yes |
Yes |
Yes |
Yes |
difference_type |
Yes |
Yes |
Yes |
No |
reference |
Yes |
Yes |
Yes |
No |
const_reference |
Yes |
Yes |
Yes |
No |
iterator |
Yes |
Yes |
Yes |
No |
const_iterator |
Yes |
Yes |
Yes |
No |
reverse_iterator |
Yes |
Yes |
Yes |
No |
const_reverse_iterator |
Yes |
Yes |
Yes |
No |
pointer |
Yes |
Yes |
Yes |
No |
const_pointer |
Yes |
Yes |
Yes |
No |
value_type |
Yes |
Yes |
Yes |
Yes |
allocator_type |
Yes |
Yes |
Yes |
No |
mapped_type |
No |
Yes |
No |
No |
key_type |
No |
Yes |
Yes |
No |
key_compare |
No |
Yes |
Yes |
No |
value_compare |
No |
No |
Yes |
No |
container_type |
No |
No |
No |
Yes |
What does each typedef alias represent?
- size_type
- An unsigned integer type, available in any first-class container
class, and in any container adaptor class, and sufficiently large to
hold the size of any object of that class.
- difference_type
- An unsigned integer type, available in any first-class container
class, and sufficiently large to hold the maximum difference between
two address values of two components of an object of the given
class.
- reference
- A reference to a component of a container object (i.e.,
T&, where T is the component type of the
container object).
- const_reference
- A const reference to a component of a container object
(i.e., const T&, where T is the component type
of the container object).
- iterator
- An iterator of the default type for a (first-class) container
type.
- const_iterator
- A const iterator of the default type for a (first-class)
container type.
- reverse_iterator
- A reverse iterator of the default type for a (first-class)
container type.
- const_reverse_iterator
- A const reverse iterator of the default type for a
(first-class) container type.
- pointer
- A pointer to a component of a (first-class) container object
(i.e., same as T*, where T is the component type of
the container object).
- const_pointer
- A const pointer to a component of a (first-class)
container object (i.e., same as const T*, where T
is the component type of the container object).
- value_type
- The same type as the type of the values stored in a container
object (i.e., the same as T for sequential containers and
container adaptors, but pair<const KType, VType>
for associative containers).
- allocator_type
- The type of the allocator used for the container class
(Allocators are the most infrequently used feature of the STL by the
average C++ programmer. It is almost always OK to go with the
"default allocator" for whatever container you are using.)
- mapped_type
- The same type as VType in pair<KType,
VType> for map and multimap container types.
- key_type
- The same type as KType in any associative container
class. For sets and multisets, it is equivalent to
value_type.
- key_compare
- The type of the comparitor in any associative container.
- value_compare
- The type of the comparitor for the whole component type in any
associative container. For sets and multisets, it is equivalent to
key_compare. For maps and multimaps it is an auxiliary
comparitor class that compares just the keys of two components.
- container_type
- The type of first-class container upon which a container adaptor
is based.