6. Library Overview

Why waste time learning
when ignorance is instantaneous?

– Hobbes

Introduction

Standard-Library Components

Standard-Library Headers and Namespace

Advice

6.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 1-5, Chapters 6-13 give a quick tour of key standard-library facilities. I very briefly present useful standard-library types, such as string, ostream, vector, map,

unique_ptr, thread, regex, and complex, as well as the most common ways of using them. As in Chapters 1-5, 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 almost 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.

6.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 and localization); see §7.2.

• Support for regular expression matching; see §7.3.

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

• A framework of containers (such as vector and map) and algorithms (such as find(), sort(), and merge()); see Chapter 9 and Chapter 10. 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 12.

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

• Utilities to support template metaprogramming (e.g., type traits; §11.6), STL-style generic programming (e.g., pair; §11.3.3), and general programming (e.g., clock; §11.4).

• “Smart pointers” for resource management (e.g., unique_ptr and shared_ptr; §11.2.1) and an interface to garbage collectors (§4.6.4).

• Special-purpose containers, such as array11.3.1), bitset11.3.2), and tuple11.3.3).

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

• that 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.

6.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.3) called std. To use standard library facilities, the std:: prefix can be used:

std::string s {"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:

Image

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.

6.4. Advice

[1] The material in this chapter roughly corresponds to what is described in much greater detail in Chapter 30 of [Stroustrup,2013].

[2] Don’t reinvent the wheel; use libraries; §6.1.

[3] When you have a choice, prefer the standard library over other libraries; §6.1.

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

[5] Remember to #include the headers for the facilities you use; §6.3.

[6] Remember that standard-library facilities are defined in namespace std; §6.3.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.140.206