Chapters 1–8 built the language from first principles: types, classes, templates, concepts.
But no significant program is written in a bare programming language — "first, a set of
libraries is developed. These then form the basis for further work." This chapter is the pivot
of the book: a tour of the standard library, whose specification is over two
thirds of the ISO C++ standard. It classifies what the library offers (strings, I/O streams,
containers, algorithms, ranges, numerics, concurrency, smart pointers…), and shows how it's
organized: everything lives in namespace std, delivered through headers (soon
modules), with literal suffixes quarantined in sub-namespaces and range-versions of algorithms
kept separate from the iterator-pair versions.
Why the library matters now: everything you'll meet from here on —
string, vector, map, sort(),
unique_ptr, thread — is
already built from the tools you now know: classes, RAII, templates, concepts.
Stroustrup's advice is blunt: explore it, and prefer it to home-made alternatives.
Much thought went into its design, more into its implementations, and much effort into its
maintenance. The interview takeaway is the organization — namespace
std, the sub-namespace suffix rules, and why
ranges::sort(v) exists alongside sort(v.begin(), v.end()).
Most programs are tedious to write in the bare language, whereas just about any task can be
rendered simple by the use of good libraries. Chapters 9–18 give a quick tour of key
standard-library facilities:
string, ostream, variant, vector,
map, path, unique_ptr, thread,
regex, system_clock, time_zone, and
complex. As in Chapters 1–8, don't be distracted or discouraged by incomplete
understanding of details — the goal is a basic understanding of the most useful facilities.
Three selection criteria governed whether a class was included in the standard library:
Essentially, the C++ standard library provides the most common fundamental data structures together with the fundamental algorithms used on them. The standard-library facilities are part of every complete C++ implementation — and beyond them lie GUIs, web interfaces, database interfaces, and thousands of specialized libraries, none of which this book describes. The intent is a self-contained, portable description of C++ as defined by its standard [C++, 2020].
The facilities provided by the standard library can be classified like this:
vector, map;
Chapter 12) and algorithms (find(), sort(), merge();
Chapter 13) [Stepanov, 1994] — extensible, so users can add their own containers and
algorithms.
threads and locks (Chapter 18),
foundational so users can add new concurrency models as libraries; plus synchronous and
asynchronous coroutines (§18.6).
sort() (§13.6) and reduce() (§17.3.1).
pair; §15.3.3), and general programming (variant,
optional; §15.4).
unique_ptr,
shared_ptr (§15.2.1).
array (§15.3.1),
bitset (§15.3.2), tuple (§15.3.3).
time_point,
system_clock (§16.2.1), month,
time_zone (§16.2.2–§16.2.3).
ms for milliseconds, i for
imaginary (§6.6).
string_views (§10.3),
spans (§15.2.2).
In one sentence: the standard library provides the most common fundamental data structures and the fundamental algorithms used on them, each checked against the three inclusion criteria above.
The facilities of the standard library are placed in namespace
std and made available to users through modules or header files.
Every standard-library facility is provided through some standard header:
#include <string> // the standard string
#include <list> // the standard list
std::string sheep {"Four legs Good; two legs Baaad!"};
std::list<std::string> slogans {"War is Peace", "Freedom is Slavery", "Ignorance is Strength"};
The library is defined in the namespace std (§3.3). You can use the
std:: prefix, or — at some cost in taste — bring the names in:
#include <string>
using namespace std; // make std names available without std:: prefix
string s {"C++ is a general-purpose programming language"}; // OK: string is std::string
It is generally in poor taste to dump every name from a namespace into the global namespace — but the book does it for brevity, and it's good to know what the library offers.
The standard library offers several sub-namespaces that can be accessed only through an explicit action:
| Sub-namespace | Contents |
|---|---|
std::chrono |
all chrono facilities, including std::literals::chrono_literals (§16.2)
|
std::literals::chrono_literals |
suffixes y (years), d (days), h, min,
s, ms, us, ns (§16.2)
|
std::literals::complex_literals |
suffixes i, if, il for imaginary (§6.6) |
std::literals::string_literals |
suffix s for strings (§6.6, §10.2) |
std::literals::string_view_literals |
suffix sv for string views (§10.3) |
std::numbers |
mathematical constants (§17.9) |
std::pmr |
polymorphic memory resources (§12.7) |
To use a suffix from a sub-namespace, you must introduce it into the namespace where you want to use it:
// no mention of complex_literals
auto z1 = 2+3i; // error: no suffix 'i'
using namespace literals::complex_literals; // make the complex literals visible
auto z2 = 2+3i; // ok: z2 is a complex<double>
Why sub-namespaces at all? Suffixes cannot be explicitly qualified (you can't write
chrono_literals::s — a suffix is only usable after a literal), so the only way to
use one is to bring in a whole set. Placing them in sub-namespaces means you can bring in
one set of suffixes into a scope without risking ambiguities with another library's
suffixes.
The standard library offers algorithms such as sort() and copy() in
two versions:
sort(begin(v), v.end()).
sort(v).Ideally these would overload perfectly without special effort — but they don't. Naively bringing in both namespaces makes both calls ambiguous:
using namespace std;
using namespace ranges;
void f(vector<int>& v) {
sort(v.begin(), v.end()); // error: ambiguous
sort(v); // error: ambiguous
}
To protect against ambiguities with the traditional unconstrained templates, the standard requires that you explicitly introduce the range version of an algorithm into a scope:
using namespace std;
void g(vector<int>& v) {
sort(v.begin(), v.end()); // OK (the std iterator-pair version)
sort(v); // error: no matching function (in std)
ranges::sort(v); // OK (explicitly qualified)
using ranges::sort; // from here on:
sort(v); // OK (resolves to the range version)
}
There are not yet any standard-library modules — a gap caused by lack of committee time,
likely remedied in C++23. The book uses a hypothetical
import std; module that would offer all facilities from namespace
std (see Appendix A). The advice: prefer importing modules over
#includeing header files when they become available — modules are semi-compiled
and fast to import (§8.5).
Here is a selection of standard-library headers, all supplying declarations in namespace
std:
| Header | Key facilities | Section |
|---|---|---|
<algorithm> |
copy(), find(), sort() |
Ch. 13 |
<array> |
array |
§15.3.1 |
<chrono> |
duration, time_point, month, time_zone
|
§16.2 |
<cmath> |
sqrt(), pow() |
§17.2 |
<complex> |
complex, sqrt(), pow() |
§17.4 |
<concepts> |
floating_point, copyable, predicate,
invocable
|
§14.5 |
<filesystem> |
path |
§11.9 |
<format> |
format() |
§11.6.2 |
<fstream> |
fstream, ifstream, ofstream |
§11.7.2 |
<functional> |
function, greater_equal, hash,
range_value_t
|
Ch. 16 |
<future> |
future, promise |
§18.5 |
<ios> |
hex, dec, scientific, fixed,
defaultfloat
|
§11.6.2 |
<iostream> |
istream, ostream, cin, cout |
Ch. 11 |
<map> |
map, multimap |
§12.6 |
<memory> |
unique_ptr, shared_ptr, allocator |
§15.2.1 |
<random> |
default_random_engine, normal_distribution |
§17.5 |
<ranges> |
sized_range, subrange, take(),
split(), iterator_t
|
§14.1 |
<regex> |
regex, smatch |
§10.4 |
<string> |
string, basic_string |
§10.2 |
<string_view> |
string_view |
§10.3 |
<set> |
set, multiset |
§12.8 |
<sstream> |
istringstream, ostringstream |
§11.7.3 |
<stdexcept> |
length_error, out_of_range, runtime_error |
§4.2 |
<thread> |
thread |
§18.2 |
<tuple> |
tuple, get<>(), tuple_size<> |
§15.3.4 |
<unordered_map> |
unordered_map, unordered_multimap |
§12.6 |
<utility> |
move(), swap(), pair |
Ch. 16 |
<variant> |
variant |
§15.4.1 |
<vector> |
vector |
§12.2 |
This listing is far from complete. Headers from the C standard library (like
<stdlib.h>) are provided too — and for each, there is a version with the
name prefixed by c and the .h removed. That version
(<cstdlib>) places its declarations in both the
std and global namespaces. The headers reflect the history of the library's
development, so they aren't always as logical and easy to remember as we'd like — one reason
to use a module such as std (§9.3.3) instead.
Here is a summary of the guidance from this chapter. All 7 items, with the section where each is introduced. The C++ Core Guidelines link each item to its recommended practice.
| # | Guideline | § |
|---|---|---|
| 1 | Don't reinvent the wheel; use libraries. | 9.1 |
| 2 | When you have a choice, prefer the standard library over other libraries. | 9.1 |
| 3 | Do not think that the standard library is ideal for everything. | 9.1 |
| 4 |
If you don't use modules, remember to #include the appropriate headers.
|
9.3.1 |
| 5 |
Remember that standard-library facilities are defined in namespace std.
|
9.3.1 |
| 6 | When using ranges, remember to explicitly qualify algorithm names. |
9.3.2 |
| 7 | Prefer importing modules over #includeing header files. |
9.3.3 |
Which is the strongest reason to prefer a standard-library facility over a home-made alternative?
Why must literal suffixes like s, ms, and i be
brought in via sub-namespaces (e.g.,
using namespace literals::complex_literals;)?
With using namespace std; alone, why does sort(v); fail while
ranges::sort(v); works?
What is the difference between <stdlib.h> and
<cstdlib>?
In Stroustrup's classification, what does "the STL framework" refer to?
Which is NOT one of the three criteria for including a class in the standard library?
no significant program in bare language ⇒ libraries first
std lib ⇒ > 2/3 of ISO C++ standard ; part of every complete implementation
prefer std ⇒ design + implementation + maintenance effort ; × reinvent wheel
std lib × ideal for everything ⇒ specialized domains need specialized libs
3 criteria ⇒ helpful to almost everyone | no significant overhead | simple uses easy to learn
language support ⇒ allocation, exceptions, RTTI
C stdlib ⇒ with minor type-safety modifications
strings ⇒ + regex + views (string_view)
streams ⇒ extensible I/O framework → user types, locales, formatting
STL ⇒ containers + algorithms framework → extensible [Stepanov]
ranges ⇒ views, generators, pipes ; concepts (§14.5)
numerics ⇒ math fns, complex, constants, random
concurrency ⇒ threads, locks, coroutines, parallel algorithms
smart pointers ⇒ unique_ptr, shared_ptr ; special containers ⇒ array, bitset, tuple
time ⇒ time_point, system_clock, month, time_zone ; unit suffixes
everything ⇒ namespace std ; via headers or modules
using namespace std; ⇒ poor taste but common
sub-namespaces ⇒ chrono_literals, complex_literals,
string_literals, string_view_literals, numbers,
pmr
suffix × explicitly qualified ⇒ bring in a set → sub-namespace avoids ambiguity
(eg using namespace literals::complex_literals; then
2+3i works)
2 versions ⇒ iterator-pair (eg sort(v.begin(),v.end()))
| range (eg sort(v))
both namespaces → ambiguous ; must introduce explicitly
ranges::sort(v) ⇒ qualified ; using ranges::sort;
→ then sort(v) OK
std modules × yet ⇒ C++23 likely ; import std; hypothetical
prefer import ⇒ semi-compiled, fast to import
headers ⇒ selection table (eg <vector>,
<algorithm>, <memory>, <chrono>)
C headers ⇒ <stdlib.h> global |
<cstdlib> std + global
headers reflect history ⇒ not always logical → modules the fix
Primary source: Stroustrup, B. (2022). A Tour of C++, 3rd ed.,
Chapter 9: "Library Overview." Addison-Wesley.
Reference:
Chapter 1–9 Quick Reference & Glossary —
keep it beside you while you study.
Recommended supplement: cppreference on
standard library headers,
ranges library,
namespaces, and
user-defined literals.
Questions? Ask your agent — your teacher — about anything unclear: how the ranges versions coexist with the traditional algorithms, when a sub-namespace is warranted, or which header provides what. Follow-ups are expected, not optional.