Standard Library Header Files - Professional C++ (2014)

Professional C++ (2014)

Appendix CStandard Library Header Files

The interface to the C++ Standard Library consists of 80 header files, 26 of which present the C standard library. It’s often difficult to remember which header files you need to include in your source code, so this material provides a brief description of the contents of each header, organized into eight categories:

· The C Standard Library

· Containers

· Algorithms, iterators, and allocators

· General utilities

· Mathematical utilities

· Exceptions

· I/O Streams

· Threading library

THE C STANDARD LIBRARY

The C++ Standard Library includes the entire C Standard Library. The header files are generally the same, except for two points:

· The header names are <cname> instead of <name.h>.

· All the names declared in the <cname> header files are in the std namespace.

NOTE For backward compatibility, you can still include <name.h> if you want. However, that puts the names into the global namespace instead of the std namespace, and on top of that, the use of <name.h> has been deprecated. It is recommended to avoid this feature.

The following table provides a summary of the most useful functionality. Note that it’s recommended to avoid using C functionality, and instead use equivalent C++ features whenever possible.

HEADER FILE NAME

CONTENTS

<cassert>

assert() macro.

<ccomplex>

Utilities to work with complex numbers.

<cctype>

Character predicates and manipulation functions, such as isspace() and tolower().

<cerrno>

Defines errno expression, a macro to get the last error number for certain C functions.

<cfenv>

Supports the floating-point environment, such as floating-point exceptions, rounding, and so on.

<cfloat>

C-style defines related to floating-point arithmetic, such as FLT_MAX.

<cinttypes>

Defines a number of macros to use with the printf(), scanf() and similar functions. Also includes a few functions to work with intmax_t.

<ciso646>

In C, the <iso646.h> file defines macros and, or, etc. In C++, those are keywords, so this header is empty.

<climits>

C-style limit defines, such as INT_MAX.

<clocale>

A few localization macros and functions like LC_ALL and setlocale().

<cmath>

Math utilities, including trigonometric functions, sqrt(), fabs(), and others.

<csetjmp>

setjmp() and longjmp(). Never use these in C++!

<csignal>

signal() and raise(). Avoid these in C++.

<cstdalign>

Alignment related macro __alignas_is_defined.

<cstdarg>

Macros and types for processing variable-length argument lists.

<cstdbool>

Boolean type related macro __bool_true_false_are_defined.

<cstddef>

Important constants such as NULL, and important types such as size_t.

<cstdint>

Defines a number of standard integer types such as int8_t, int64_t and so on. It also includes macros specifying minimum and maximum values of those types.

<cstdio>

File operations, including fopen() and fclose(). Formatted I/O: printf(), scanf(), and family. Character I/O: getc(), putc(), and family. File positioning: fseek(), ftell().

<cstdlib>

Random numbers with rand() and srand(). The abort() and exit() functions, which you should avoid. C-style memory allocation functions: calloc(), malloc(), realloc(), free(). C-style searching and sorting with qsort() and bsearch(). String to number conversions: atof(), atoi(), etc. A set of functions related to multibyte/wide strings manipulation.

<cstring>

Low-level memory management functions, including memcpy() and memset(). C-style string functions, such as strcpy() and strcmp().

<ctgmath>

Just includes <ccomplex> and <cmath>.

<ctime>

Time-related functions, including time() and localtime().

<cuchar>

Defines a number of Unicode-related macros, and functions like mbrtoc16().

<cwchar>

Versions of string, memory, and I/O functions for wide characters.

<cwctype>

Versions of functions in <cctype> for wide characters: iswspace(), towlower(), and so on.

CONTAINERS

The definitions for the STL containers can be found in 12 header files:

HEADER FILE NAME

CONTENTS

<array>

The array class template.

<bitset>

The bitset class template.

<deque>

The deque class template.

<forward_list>

The forward_list class template.

<list>

The list class template.

<map>

The map and multimap class templates.

<queue>

The queue and priority_queue class templates.

<set>

The set and multiset class templates.

<stack>

The stack class template.

<unordered_map>

The unordered_map and unordered_multimap class templates.

<unordered_set>

The unordered_set and unordered_multiset class templates.

<vector>

The vector class template and the vector<bool> specialization.

Each of these header files contains all the definitions you need to use the specified container, including iterators. Chapter 16 describes these containers in detail.

ALGORITHMS, ITERATORS, AND ALLOCATORS

The “rest” of the STL can be found in six different header files:

HEADER FILE NAME

CONTENTS

<algorithm>

Prototypes for most of the algorithms in the STL. See Chapter 17.

<functional>

Defines the built-in function objects, negators, binders, and adaptors. See Chapter 17.

<iterator>

Definitions of iterator_traits, iterator tags, iterator, reverse_iterator, insert iterators (such as back_inserter), and stream iterators. See Chapter 20.

<memory>

Defines the default allocator, functions for dealing with uninitialized memory inside containers, unique_ptr, shared_ptr, make_unique(), and make_shared() introduced in Chapter 1.

<numeric>

Prototypes for some numerical algorithms: accumulate(), inner_product(), partial_sum(), adjacent_difference(), and a few others. See Chapter 17.

<scoped_allocator>

An allocator that can be used with nested containers such as a vector of strings, or a vector of maps.

GENERAL UTILITIES

The Standard Library contains some general-purpose utilities in several different header files:

HEADER FILE NAME

CONTENTS

<chrono>

Defines the Chrono library. See Chapter 19.

<codecvt>

Provides code conversion facets for various character encodings.

<initializer_list>

Defines the initializer_list class. See Chapter 10.

<limits>

Defines the numeric_limits class template, and specializations for most built-in types. See Chapter 15.

<locale>

Defines the locale class, the use_facet() and has_facet() function templates, and the various facet families. See Chapter 18.

<new>

Defines the bad_alloc exception and set_new_handler() function. Prototypes for all six forms of operator new and operator delete. See Chapter 14.

<random>

Defines the random number generation library. See Chapter 19.

<ratio>

Defines the Ratio library to work with compile-time rational numbers. See Chapter 19.

<regex>

Defines the regular expression library. See Chapter 18.

<string>

Defines the basic_string class template and the typedef instantiations of string and wstring.

<system_error>

Defines error categories and error codes.

<tuple>

Defines the tuple class template as a generalization of the pair class template. See Chapter 19.

<type_traits>

Defines type traits for use in template metaprogramming. See Chapter 21.

<typeindex>

Defines a simple wrapper for type_info, which can be used as an index type in associative containers and in unordered associative containers.

<typeinfo>

Defines the bad_cast and bad_typeid exceptions. Defines the type_info class, objects of which are returned by the typeid operator. See Chapter 9 for details on typeid.

<utility>

Defines the pair class template. See Chapter 16.

MATHEMATICAL UTILITIES

C++ provides some facilities for numeric processing. These capabilities are not described in detail in this book; for details, consult one of the Standard Library references listed in the Annotated Bibliography.

HEADER FILE NAME

CONTENTS

<complex>

Defines the complex class template for working with complex numbers.

<valarray>

Defines valarray and related classes and class templates for working with mathematical vectors and matrices.

EXCEPTIONS

Exceptions and exception support are covered in Chapter 13. Two header files provide most of the requisite definitions, but some exceptions for other domains are defined in the header file for that domain.

HEADER FILE NAME

CONTENTS

<exception>

Defines the exception and bad_exception classes, and the set_unexpected(), set_terminate(), and uncaught_exception() functions.

<stdexcept>

Non-domain-specific exceptions not defined in <exception>.

I/O STREAMS

The following table lists all the header files related to I/O streams in C++. However, normally your applications only need to include <fstream>, <iomanip>, <iostream>, <istream>, <ostream>, and <sstream>. Consult Chapter 12 for details.

HEADER FILE NAME

CONTENTS

<fstream>

Defines the basic_filebuf, basic_ifstream, basic_ofstream, and basic_fstream classes. Declares the filebuf, wfilebuf, ifstream, wifstream, ofstream, wofstream, fstream, and wfstream typedefs.

<iomanip>

Declares the I/O manipulators not declared elsewhere (mostly in <ios>).

<ios>

Defines the ios_base and basic_ios classes. Declares most of the stream manipulators. You rarely have to include this header directly.

<iosfwd>

Forward declarations of the templates and typedefs found in the other I/O stream header files. You rarely need to include this header directly.

<iostream>

Declares cin, cout, cerr, clog, and the wide-character counterparts. Note that it’s not just a combination of <istream> and <ostream>.

<istream>

Defines the basic_istream and basic_iostream classes. Declares the istream, wistream, iostream, and wiostream typedefs.

<ostream>

Defines the basic_ostream class. Declares the ostream and wostream typedefs.

<sstream>

Defines the basic_stringbuf, basic_istringstream, basic_ostringstream, and basic_stringstream classes. Declares the stringbuf, wstringbuf, istringstream, wistringstream, ostringstream, wostringstream, stringstream, and wstringstream typedefs.

<streambuf>

Defines the basic_streambuf class. Declares the typedefs streambuf and wstreambuf. You rarely have to include this header directly.

<strstream>

Deprecated.

THREADING LIBRARY

C++ includes a threading library, which allows you to write platform-independent multithreaded applications. See Chapter 23 for details. The threading library consists of the following header files:

HEADER FILE NAME

CONTENTS

<atomic>

Defines the atomic types, atomic<T>, and atomic operations.

<condition_variable>

Defines the condition_variable and condition_variable_any classes.

<future>

Defines future, promise, packaged_task and async().

<mutex>

Defines call_once() and the different mutex and lock classes, except shared_timed_mutex and shared_lock.

<shared_mutex>

Defines the shared_timed_mutex and shared_lock classes.