C++ Reference Material
STL Headers
In order to make use of any STL facility (container, algorithm, or whatever) you must of course be sure to include the header file that provides access to the facility you want. This page contains six tables, showing which header file you would have to include in your source file to provide your code with access to a particular container, iterator feature, algorithm, function object, or other feature. Unfortunately, the C++ Standard does not specify which headers may, should or must be included in other headers, so implementations will differ in this area. For example, you will generall find the <utility> header automatically included in the <map> header so that maps and multimaps have ready access to the pair struct.
It is always a good idea to include the header where you know something you are using lives, even if its inclusion is redundant. Where not doing this may come back to bite you is when you move your code to a different platform, re-compile, and that "redundant" header on the first platform is not redundant on the new platform, giving rise to a something-not-found compile-time error.
There are seven standard "first-class" container classes (three sequential and four associative) and three container adaptor classes, but only seven header files that provide access to these containers or container adaptors, as shown in the table below.
Header | Container Class(es) or
Container Adaptor Class(es) Provided |
Container Category |
---|---|---|
<vector> | vector | sequential
(first-class) |
<deque> | deque | |
<list> | list | |
<map> | map, multimap | associative
(first-class) |
<set> | set, multiset | |
<stack> | stack | adaptor |
<queue> | queue, priority_queue |
Somewhat counterintuitively, even though as an STL programmer you will be using STL iterators virtually every time you write some STL code, you will not often need to include the <iterator> header. In particular, you will not need to include this header to get the usual kinds of iterators you use to work with the STL containers. The reason for this is that each container class provides its own iterators, needs the contents of <iterator> to do so, and therefore itself includes <iterator>. Thus, by including <vector> (for example) you get <iterator> "for free".
Header | Iterator Classes Provided | Iterator Functions Provided |
---|---|---|
<iterator> | iterator
reverse_iterator iterator_traits insert_iterator back_insert_iterator front_insert_iterator istream_iterator ostream_iterator istreambuf_iterator ostreambuf_iterator |
advance()
distance() back_inserter() front_inserter() inserter() |
The table below is deceptively small, and effectively hides the fact that there are nearly seventy algorithms available from the <algorithm> header, along with the four from the <numeric> header explicitly shown.
Header | Algorithms Provided |
---|---|
<algorithm> | All except those in <numeric> |
<numeric> | accumulate
adjacent_difference inner_product partial_sum |
The following table gives a list of built-in functors, binders, negators, and function adaptors provided in the <functional> header. A functor (function object) is an object of a class that implements operator(), which allows the object to be used like a function, but is a more versatile (and useful) than a simple function since (being an object) it also has a state. Binders and negators can modify the behavior of functors in useful ways, and function adaptors allow you to adapt "old-fashioned" function pointers to a form that can be used by various STL components.
Header | Facilities Provided | ||||
---|---|---|---|---|---|
Functors | Binders | Negators | Function Adaptors | ||
<functional> | arithmetic | bind_1st
bind_2nd |
not1
not2 |
ptr_fun
mem_fun mem_fun1 mem_fun_ref mem_fun1_ref |
|
binary | unary | ||||
plus
minus multiplies divides modulus |
negate | ||||
relational | |||||
binary | unary | ||||
equal_to
not_equal_to greater greater_equal less less_equal logical_and logical_or |
|||||
logical | |||||
binary | unary | ||||
logical_and
logical_or |
logical_not |
This header declares the pair<> template, which is essential when using maps and multimaps, but also finds many uses in everyday programming. Though it may or may not be of interest to the average programmer, this header also defines the rel-ops namespace, which in turn defines relational operators in terms of == and <.
Header | Utilities Provided |
---|---|
<utility> | The pair struct
The make_pair() function |
This header declares functions and class templates for allocating and using memory. The average programmer will not need to include this header, since the default container "allocators" are perfectly adequate most of the time. Programmers who are developing their own containers, iterators and algorithms will be more inclined to find a use for what's in this header. If anything, the feature most likely to be used by the average programmer is auto_ptr
Header | Memory Facilities Provided |
---|---|
<memory> | auto_ptr |