Professional C__ - Marc Gregoire [35]
The C++ Standard Library
The most important library that you will use as a C++ programmer is the C++ standard library. As its name implies, this library is part of the C++ standard, so any standards-conforming compiler should include it. The standard library is not monolithic: It includes several disparate components, some of which you have been using already. You may even have assumed they were part of the core language. Chapters 11 to 17 will go into more details about the standard library.
C Standard Library
Because C++ is a superset of C, the entire C library is still available. Its functionality includes mathematical functions such as abs(), sqrt(), and pow(), random numbers with srand() and rand(), and error-handling helpers such as assert() and errno. Additionally, the C library facilities for manipulating character arrays as strings, such as strlen() and strcpy(), and the C-style I/O functions, such as printf() and scanf(), are all available in C++.
C++ provides better strings and I/O support than C. Even though the C-style strings and I/O routines are available in C++, you should avoid them in favor of C++ strings (Chapter 14) and I/O streams (Chapter 15).
This book assumes that you are familiar with the C libraries. If not, consult one of the C reference books listed in Appendix B. Note also that the C header files have different names in C++. These names should be used instead of the more familiar C library names, because they are less likely to result in name conflicts. For details, see the Standard Library Reference resource on the website.
Deciding Whether or Not to Use the STL
The STL was designed with functionality, performance, and orthogonality as its priorities. The benefits of using it are substantial. Think about the number of times you’ve tracked down pointer errors in linked list or balanced binary tree implementations, or debugged a sorting algorithm that wasn’t sorting properly. If you use the STL correctly, you will rarely, if ever, need to perform that kind of coding again. Chapters 11 to 17 provide in-depth information on the STL functionality.
DESIGNING WITH PATTERNS AND TECHNIQUES
Learning the C++ language and becoming a good C++ programmer are two very different things. If you sat down and read the C++ standard, memorizing every fact, you would know C++ as well as anybody else. However, until you gain some experience by looking at code and writing your own programs, you wouldn’t necessarily be a good programmer. The reason is that the C++ syntax defines what the language can do in its raw form, but doesn’t say anything about how each feature should be used.
As they become more experienced in using the C++ language, C++ programmers develop their own individual ways of using the features of the language. The C++ community at large has also built some standard ways of leveraging the language, some formal and some informal. Throughout this book, the authors point out these reusable applications of the language, known as design techniques and design patterns. Additionally, Chapters 28 and 29 focus almost exclusively on design techniques and patterns. Some patterns and techniques will seem obvious to you because they are simply a formalization of the obvious solution. Others describe novel solutions to problems you’ve encountered in the past. Some present entirely new ways of thinking about your program organization.
It is important for you to familiarize yourself with these patterns and techniques so that you can recognize when a particular design problem calls for one of these solutions. There are many more techniques and patterns applicable to C++ than those described in this book. Although the authors feel