Using the C++ Standard Library
Every standard library facility is provided through some standard header.
A Program That Depends on Its Include Lines
Look at the short program below. It uses two standard library facilities, a string and a list, and it does not define either of them itself. Before reading the explanation, observe the top of the file. Two lines begin with the word include. Each one names a header inside angle brackets. Your job at this point is only to notice the moment where a facility becomes available to the code.
The program prints nothing. It exists to show that two standard facilities are declared and used.What do you think happens?
Suppose you delete the line #include <list> and leave everything else the same. Does the program still compile as written?
Reveal answer
No. The program no longer compiles as written.
A standard library facility is made available by including the header responsible for it. Without the list header, the name std::list is not made available to the program, so the line that uses it cannot be compiled. The string line is unaffected, because its own include line is still there.
What the C++ Standard Library Is
The C++ Standard Library is the shared foundation that most C++ programs are built on. It is a collection of facilities, such as the standard string and the standard list, that the language standard describes and that every complete implementation provides.
Consider a task such as reading a list of names from a file, removing duplicates, sorting them, and printing them in order. You could write every piece yourself from the most basic operations the language offers. Real programs almost always take a different route. They draw on ready-made building blocks that already handle storage of collections, text, and output. Nothing significant is built from the bare language alone. Developers first assemble a set of libraries, and those libraries become the foundation for everything written afterward.
This matters in practice. Tasks that would be laborious when expressed only in the core language can become simple once good libraries are available. Learning to use the standard library effectively is therefore not an optional extra. It is how most C++ work actually gets done.
Including a Header to Get a Facility
Every standard library facility is provided through some standard header, and a facility becomes available to your program when you include the header that provides it. You do not get a facility just because your program is compiled. You get it by including the header that is responsible for it. An include directive names the header inside angle brackets, with the general form shown below, where the placeholder stands for the name of whichever standard header you need.
- #include is the directive that asks for a header
- The angle brackets surround the header name
header_nameis replaced by the exact standard name, such as string or list
The header name is a word that identifies a standard header. It is not the name of a variable or a function. You write it exactly as the standard defines it, and you place the directive so that the header is in effect for the code that needs the facility.
Tracing a Header to Its Facility
The key idea is a one-to-one way of thinking about access. Each header supplies its own facility. The string header makes the standard string available, and the list header makes the standard list available. Because the two routes are separate, you can include one without the other, and only the facility tied to the included header becomes available. The diagram below shows this mapping for the facilities named in the introductory tour, with the header that provides each one.
Read the map in the lookup direction. Start from the facility you need and find the header that provides it, then write that header in an include line. The header names listed here are the ones the tour introduces. The tour names the facilities without describing their internal workings, so the diagram tells you only which header to ask for, not how the facility is built.
#include <string>
int main() {
std::string greeting = "hello";
std::list<int> numbers = {1, 2, 3};
return 0;
}Only the string facility is available. The name std::list has not been made available, so the list line fails.
#include <string>
#include <list>
int main() {
std::string greeting = "hello";
std::list<int> numbers = {1, 2, 3};
return 0;
}Both the string and list facilities are available, because each header contributed its own facility.
How an Include Line Takes Effect
An include line is a statement about which facility you are bringing into the file. It is handled before the rest of your code is compiled, and its effect is that the facilities tied to that header become usable in the code that follows the directive. The flowchart below follows one include line from the source file through to a program that uses the facility.
The position of the directive matters. The source says the directive is placed so that the header is in effect for the code that needs the facility. Put the include lines at the top of the file, before the code that depends on them, so that the facilities are in effect where they are used.
Mistakes Beginners Make with Includes
Common mistake Assuming a facility works because the program was compiled
What it looks likeUsing std::list in a file that has no #include <list> line and expecting it to compile
Why it failsA facility is not made available just by compiling. It is made available by including the header responsible for it.
Do this insteadAdd the include line for the header that provides the facility, such as #include <list> for std::list.
Common mistake Including the wrong header for the facility
What it looks likeWriting #include <string> and then using std::list
Why it failsEach header supplies its own facility. The string header does not make the list facility available.
Do this insteadLook up which header provides the facility you need and include that header.
Common mistake Misspelling the header name
What it looks likeWriting the directive with a name that is not the exact standard header name
Why it failsThe header name must be written exactly as the standard defines it. It is a name, not a loose description.
Do this insteadWrite the header name exactly as the standard defines it, inside angle brackets.
Common mistake Believing one include line brings in every standard facility at once
Diagnosing a Missing Header
When a program using standard facilities does not behave as you expect, the cause is often in the include lines at the top of the file. A compile error about an unknown name is a common sign. The flowchart below gives a sequence of checks to decide whether a missing or wrong include is the cause.
Standard Library Versus System Features
It helps to see the standard library as one layer among several things a complete environment may provide. The standard-library facilities are part of every complete C++ implementation. Most implementations also offer graphical user interface systems, web interfaces, and database interfaces. Application-development environments often add their own foundation libraries for corporate or industrial development and execution. The introductory tour does not describe those systems and libraries, so keep them distinct from the facilities covered here.
| Feature | Belongs to the standard library? | Covered by this tour? |
|---|---|---|
| string and list | Yes, provided through standard headers | Yes |
| ostream, vector, map | Yes, provided through standard headers | Yes |
| GUI systems | No, offered by most implementations as separate systems | No |
| Web interfaces | No, offered by most implementations as separate systems | No |
| Database interfaces | No, offered by most implementations as separate systems | No |
| Corporate foundation libraries | No, added by application-development environments | No |
Standard library facilities versus system features outside the tour
Why Prefer Standard Facilities
The source gives three reasons for preferring standard-library facilities to home-made alternatives. First, much thought has gone into their design. Second, more thought has gone into their implementations. Third, substantial effort will continue to go into their maintenance and extension. Each reason is about the life of the code, not only the first day it is written. A home-made version has to be designed, implemented, and maintained by the program's authors alone, while a standard facility carries effort that has already been invested and will keep being invested.
When a standard facility does the job, include its header and use the facility rather than writing a replacement. Keep the examples portable by relying on the language and standard library as specified, rather than on features unique to one environment.
Practice Finding the Right Include
A program uses std::string and std::map but has only the line #include <string> at the top. Which include line is missing, and what would you add? Explain how you know, using the header-to-facility mapping from this article.
Hints
- Look at which facilities the code uses and check whether each has a matching include line.
- Each facility is made available by its own header, so the string include does not provide the map facility.
Write the include lines for a program that uses std::vector and std::thread, and state which facility each line makes available.
Hints
- Use the angle-bracket form with the exact header name for each facility.
Key Takeaways
- Every standard library facility is provided through some standard header, and including that header is what makes the facility available to your program.
- An include directive names the header inside angle brackets, and each header supplies its own facility, so the wrong or missing header leaves the facility unavailable.
- When code does not behave as expected, check each facility used against its include line, starting with compile errors about unknown names.
- The standard library is the shared foundation for most C++ work, and it is preferred over home-made code because its design, implementation, and maintenance reflect substantial effort.
- GUI, web, database, and corporate foundation libraries are separate from the standard library and are outside the scope of this tour.