Lesson 9: Library Overview

Lesson 0009 — A Tour of C++, Chapter 9 (§9.1–§9.4)

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()).

9.1 Introduction — Why Libraries

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].

9.2 Standard-Library Components

The facilities provided by the standard library can be classified like this:

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.

9.3 Standard-Library Organization

The facilities of the standard library are placed in namespace std and made available to users through modules or header files.

9.3.1 Namespaces

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.

9.3.2 The ranges namespace

The standard library offers algorithms such as sort() and copy() in two versions:

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)
}

9.3.3 Modules

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).

9.3.4 Headers

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.

9.4 Advice

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

Retrieval Quiz

Why prefer the standard library

Which is the strongest reason to prefer a standard-library facility over a home-made alternative?

Namespace organization

Why must literal suffixes like s, ms, and i be brought in via sub-namespaces (e.g., using namespace literals::complex_literals;)?

The ranges namespace

With using namespace std; alone, why does sort(v); fail while ranges::sort(v); works?

C headers vs. c-prefixed headers

What is the difference between <stdlib.h> and <cstdlib>?

What the STL is

In Stroustrup's classification, what does "the STL framework" refer to?

Inclusion criteria

Which is NOT one of the three criteria for including a class in the standard library?


Notes

Libraries :-

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

Components :-

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

Namespaces :-

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)

Ranges :-

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

Modules/headers :-

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.

Chapters

  1. Lesson 1: The Basics (Ch. 1)
  2. Lesson 2: User-Defined Types (Ch. 2)
  3. Lesson 3: Separate Compilation (Ch. 3)
  4. Lesson 4: Error Handling (Ch. 4)
  5. Lesson 5: Classes (Ch. 5)
  6. Lesson 6: Essential Operations (Ch. 6)
  7. Lesson 7: Templates (Ch. 7)
  8. Lesson 8: Concepts and Generic Programming (Ch. 8)
  9. Lesson 9: Library Overview (Ch. 9 — this lesson)
  10. Lesson 10: Strings and Regular Expressions (Ch. 10)
  11. Lesson 11: Input and Output (Ch. 11)
  12. Lesson 12: Containers (Ch. 12)
  13. Lesson 13: Algorithms (Ch. 13)
  14. Lesson 14: Ranges (Ch. 14)
  15. Lesson 15: Pointers and Containers (Ch. 15)
  16. Lesson 16: Utilities (Ch. 16)
  17. Lesson 17: Numerics (Ch. 17)
  18. Lesson 18: Concurrency (Ch. 18)
  19. Lesson 19: History and Compatibility (Ch. 19)
← Course Dashboard ← Ch. 8: Concepts Ch. 10: Strings and Regular Expressions → Quick Reference →