8

Library Overview

Why waste time learning when ignorance is instantaneous?

– Hobbes

8.1 Introduction

No significant program is written in just a bare programming language. First, a set of libraries is developed. These then form the basis for further work. 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.

Continuing from Chapters 17, Chapters 915 give a quick tour of key standard-library facilities. I very briefly present useful standard-library types, such as string, ostream, variant, vector, map, path, unique_ptr, thread, regex, and complex, as well as the most common ways of using them.

As in Chapters 17, you are strongly encouraged not to be distracted or discouraged by an incomplete understanding of details. The purpose of this chapter is to convey a basic understanding of the most useful library facilities.

The specification of the standard library is over two thirds of the ISO C++ standard. Explore it, and prefer it to home-made alternatives. Much thought has gone into its design, more still into its implementations, and much effort will go into its maintenance and extension.

The standard-library facilities described in this book are part of every complete C++ implementation. In addition to the standard-library components, most implementations offer “graphical user interface” systems (GUIs), Web interfaces, database interfaces, etc. Similarly, most application-development environments provide “foundation libraries” for corporate or industrial “standard” development and/or execution environments. Here, I do not describe such systems and libraries. The intent is to provide a self-contained description of C++ as defined by the standard and to keep the examples portable. Naturally, a programmer is encouraged to explore the more extensive facilities available on most systems.

8.2 Standard-Library Components

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

  • Run-time language support (e.g., for allocation and run-time type information).

  • The C standard library (with very minor modifications to minimize violations of the type system).

  • Strings (with support for international character sets, localization, and read-only views of substrings); see §9.2.

  • Support for regular expression matching; see §9.4.

  • I/O streams is an extensible framework for input and output to which users can add their own types, streams, buffering strategies, locales, and character sets (Chapter 10). There is also a library for manipulating file systems in a portable manner (§10.10).

  • A framework of containers (such as vector and map) and algorithms (such as find(), sort(), and merge()); see Chapter 11 and Chapter 12. This framework, conventionally called the STL [Stepanov,1994], is extensible so users can add their own containers and algorithms.

  • Support for numerical computation (such as standard mathematical functions, complex numbers, vectors with arithmetic operations, and random number generators); see §4.2.1 and Chapter 14.

  • Support for concurrent programming, including threads and locks; see Chapter 15. The concurrency support is foundational so that users can add support for new models of concurrency as libraries.

  • Parallel versions of most STL algorithms and some numerical algorithms (e.g., sort() and reduce()); see §12.9 and §14.3.1.

  • Utilities to support template metaprogramming (e.g., type traits; §13.9), STL-style generic programming (e.g., pair; §13.4.3), general programming (e.g., variant and optional; §13.5.1, §13.5.2), and clock13.7).

  • Support for efficient and safe management of general resources, plus an interface to optional garbage collectors (§5.3).

  • “Smart pointers” for resource management (e.g., unique_ptr and shared_ptr; §13.2.1).

  • Special-purpose containers, such as array13.4.1), bitset13.4.2), and tuple13.4.3).

  • Suffixes for popular units, such as ms for milliseconds and i for imaginary (§5.4.4).

The main criteria for including a class in the library were that:

  • it could be helpful to almost every C++ programmer (both novices and experts),

  • it could be provided in a general form that did not add significant overhead compared to a simpler version of the same facility, and

  • simple uses should be easy to learn (relative to the inherent complexity of their task).

Essentially, the C++ standard library provides the most common fundamental data structures together with the fundamental algorithms used on them.

8.3 Standard-Library Headers and Namespace

Every standard-library facility is provided through some standard header. For example:

#include<string>
#include<list>

This makes the standard string and list available.

The standard library is defined in a namespace (§3.4) called std. To use standard-library facilities, the std:: prefix can be used:

std::string sheep {"Four legs Good; two legs Baaad!"};
std::list<std::string> slogans {"War is Peace", "Freedom is Slavery", "Ignorance is Strength"};

For simplicity, I will rarely use the std:: prefix explicitly in examples. Neither will I always #include the necessary headers explicitly. To compile and run the program fragments here, you must #include the appropriate headers and make the names they declare accessible. For example:

#include<string>           // make the standard string facilities accessible
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. However, in this book, I use the standard library exclusively and it is good to know what it offers.

Here is a selection of standard-library headers, all supplying declarations in namespace std:

Selected Standard Library Headers

<algorithm>

copy(), find(), sort()

Chapter 12

<array>

array

§13.4.1

<chrono>

duration, time_point

§13.7

<cmath>

sqrt(), pow()

§14.2

<complex>

complex, sqrt(), pow()

§14.4

<filesystem>

path

§10.10

<forward_list>

forward_list

§11.6

<fstream>

fstream, ifstream, ofstream

§10.7

<future>

future, promise

§15.7

<ios>

hex, dec, scientific, fixed, defaultfloat

§10.6

<iostream>

istream, ostream, cin, cout

Chapter 10

<map>

map, multimap

§11.5

<memory>

unique_ptr, shared_ptr, allocator

§13.2.1

<random>

default_random_engine, normal_distribution

§14.5

<regex>

regex, smatch

§9.4

<string>

string, basic_string

§9.2

<set>

set, multiset

§11.6

<sstream>

istringstream, ostringstream

§10.8

<stdexcept>

length_error, out_of_range, runtime_error

§3.5.1

<thread>

thread

§15.2

<unordered_map>

unordered_map, unordered_multimap

§11.5

<utility>

move(), swap(), pair

Chapter 13

<variant>

variant

§13.5.1

<vector>

vector

§11.2

This listing is far from complete.

Headers from the C standard library, such as <stdlib.h> are provided. For each such header there is also a version with its name prefixed by c and the .h removed. This version, such as <cstdlib> places its declarations in the std namespace.

8.4 Advice

[1] Don’t reinvent the wheel; use libraries; §8.1; [CG: SL.1.]

[2] When you have a choice, prefer the standard library over other libraries; §8.1; [CG: SL.2].

[3] Do not think that the standard library is ideal for everything; §8.1.

[4] Remember to #include the headers for the facilities you use; §8.3.

[5] Remember that standard-library facilities are defined in namespace std; §8.3; [CG: SL.3].