- Introduction to C++ for Java and C# Programmers
- Getting Started with C++
- Main Language Differences
- The Standard C++ Library
The Standard C++ Library
In this section, we will briefly review the Standard C++ library. Figure D.3 lists the core C++ header files. The <exception>, <limits>, <new>, and <typeinfo> headers support the C++ language; for example, <limits> allows us to test properties of the compiler's integer and floating-point arithmetic support, and <typeinfo> offers basic introspection. The other headers provide generally useful classes, including a string class and a complex numeric type. The functionality offered by <bitset>, <locale>, <string>, and <typeinfo> loosely overlaps with the QBitArray, QLocale, QString, and QMetaObject classes in Qt.
Table D.3. Core C++ library header files
Header File |
Description |
<bitset> |
Template class for representing fixed-length bit sequences |
<complex> |
Template class for representing complex numbers |
<exception> |
Types and functions related to exception handling |
<limits> |
Template class that specifies properties of numeric types |
<locale> |
Classes and functions related to localization |
<new> |
Functions that manage dynamic memory allocation |
<stdexcept> |
Predefined types of exceptions for reporting errors |
<string> |
Template string container and character traits |
<typeinfo> |
Class that provides basic meta-information about a type |
<valarray> |
Template classes for representing value arrays |
Standard C++ also includes a set of header files that deal with I/O, listed in Figure D.4. The standard I/O classes' design harks back to the 1980s and is needlessly complex, making them very hard to extend—so difficult, in fact, that entire books have been written on the subject. It also leaves the programmer with a Pandora's box of unresolved issues related to character encodings and platform-dependent binary representations of primitive data types.
Table D.4. C++ I/O library header files
Header File |
Description |
<fstream> |
Template classes that manipulate external files |
<iomanip> |
I/O stream manipulators that take an argument |
<ios> |
Template base class for I/O streams |
<iosfwd> |
Forward declarations for several I/O stream template classes |
<iostream> |
Standard I/O streams ( cin , cout , cerr , clog ) |
<istream> |
Template class that controls input from a stream buffer |
<ostream> |
Template class that controls output to a stream buffer |
<sstream> |
Template classes that associate stream buffers with strings |
<streambuf> |
Template classes that buffer I/O operations |
<strstream> |
Classes for performing I/O stream operations on character arrays |
Chapter 12 presents the corresponding Qt classes, which feature Unicode I/O as well as a large set of national character encodings and a platform-independent abstraction for storing binary data. Qt's I/O classes form the basis of Qt's inter-process communication, networking, and XML support. Qt's binary and text stream classes are very easy to extend to handle custom data types.
The early 1990s saw the introduction of the Standard Template Library (STL), a set of template-based container classes, iterators, and algorithms that slipped into the ISO C++ standard at the eleventh hour. Figure D.5 lists the header files that form the STL. The STL has a very clean, almost mathematical design that provides generic type-safe functionality. Qt provides its own container classes, whose design is partly inspired by the STL. We describe them in Chapter 11.
Table D.5. STL header files
Header File |
Description |
<algorithm> |
General-purpose template functions |
<deque> |
Double-ended queue template container |
<functional> |
Templates that help construct and manipulate functors |
<iterator> |
Templates that help construct and manipulate iterators |
<list> |
Doubly linked list template container |
<map> |
Single-valued and multi-valued map template containers |
<memory> |
Utilities for simplifying memory management |
<numeric> |
Template numeric operations |
<queue> |
Queue template container |
<set> |
Single-valued and multi-valued set template containers |
<stack> |
Stack template container |
<utility> |
Basic template functions |
<vector> |
Vector template container |
Since C++ is essentially a superset of the C programming language, C++ programmers also have the entire C library at their disposal. The C header files are available either with their traditional names (e.g., <stdio.h>) or with new-style names with a c- prefix and no .h (e.g., <cstdio>). When we use the new-style version, the functions and data types are declared in the std namespace. (This doesn't apply to macros such as ASSERT(), because the preprocessor is unaware of namespaces.) The new-style syntax is recommended if your compiler supports it.
Figure D.6 lists the C library header files. Most of these offer functionality that overlaps with more recent C++ headers or with Qt. One notable exception is <cmath>, which declares mathematical functions such as sin(), sqrt(), and pow().
Table D.6. C++ header files for C library facilities
Header File |
Description |
<cassert> |
The ASSERT() macro |
<cctype> |
Functions for classifying and mapping characters |
<cerrno> |
Macros related to error condition reporting |
<cfloat> |
Macros that specify properties of primitive floating-point types |
<ciso646> |
Alternative spellings for ISO 646 charset users |
<climits> |
Macros that specify properties of primitive integer types |
<clocale> |
Functions and types related to localization |
<cmath> |
Mathematical functions and constants |
<csetjmp> |
Functions for performing non-local jumps |
<csignal> |
Functions for handling system signals |
<cstdarg> |
Macros for implementing variable argument list functions |
<cstddef> |
Common definitions for several standard headers |
<cstdio> |
Functions for performing I/O |
<cstdlib> |
General utility functions |
<cstring> |
Functions for manipulating char arrays |
<ctime> |
Types and functions for manipulating time |
<cwchar> |
Extended multibyte and wide character utilities |
<cwctype> |
Functions for classifying and mapping wide characters |
This completes our quick overview of the Standard C++ library. On the Internet, Dinkumware offers complete reference documentation for the Standard C++ library at http://www.dinkumware.com/refxcpp.html, and SGI has a comprehensive STL programmer's guide at http://www.sgi.com/tech/stl/. The official definition of the Standard C++ library is in the C and C++ standards, available as PDF files or paper copies from the International Organization for Standardization (ISO).
In this appendix, we covered a lot of ground at a fast pace. When you start learning Qt from Chapter 1, you should find that the syntax is a lot simpler and clearer than this appendix might have suggested. Good Qt programming only requires the use of a subset of C++ and usually avoids the need for the more complex and obscure syntax that C++ makes possible. Once you start typing in code and building and running executables, the clarity and simplicity of the Qt approach will become apparent. And as soon as you start writing more ambitious programs, especially those that need fast and fancy graphics, the C++/Qt combination will continue to keep pace with your needs.