The C++ Standard Library

C++ comes with two standard libraries: the old C library (libc.lib), and the new C++ library (libcp.lib), which is logically divided into the stream library, and STL, the standard template library. Many implementations also include a pre-standard stream library for backward compatibility.

Standard library header files and the std namespace

The declarations of the functions, variables, and types in the standard libraries are distributed among some 51 header files. We must include these header files where we use the items they declare. For example, if we will be using lists, strings, and streams, we must include:

#include <list>
#include <string>
#include <iostream>

The angle brackets tell the pre-processor to look in the "usual directory" for the header file. In theory, specifying the .h extension is optional for C++ library header files[1]  (i.e., for header files containing declarations of C++ library items). We can also drop the .h for C library header files, but in this case we add the letter "c" to the beginning of the file name to indicate that it is a C library header file. For example, the C library mathematical functions (sin(), pow(), fabs(), etc.) are declared in <math.h>, so we can either write:

#include <math.h>


#include <cmath>

See your online documentation or [STR] for a list of all 51 header files.

Namespaces were discussed in Programming Note A.1.3, where it was stated that the entire standard C++ library exists in a namespace called std, and therefore standard library names should be qualified by users:

std::cout << "Hello, world!\n";

Namespaces are useful for large programs, but for small programs they get a little tedious, so lazy programmers, like us, often join the entire std namespace to each file scope with a using directive:

#include <list>
#include <string>
#include <iostream>
using namespace std;

Now we can refer to library items without the std qualification:

cout << "Hello, world!\n";

Namespaces are a relatively recent addition to C++, so of course they don't appear in the pre-standard stream libraries or the C library.

[1] Including <iostream.h> in a Visual C++ program signals that the programmer wants the pre-standard stream library, while including <iostream> signals that the standard stream library is wanted.