C++ How to Program, 4/e Tour of the Book (Continued)
Chapter 21 Standard Template Library (STL)
Throughout this book, we discuss the importance of software reuse. Recognizing that many data structures and algorithms commonly were used by C++ programmers, the C++ standard committee added the Standard Template Library (STL) to the C++ Standard Library. The STL defines powerful, template-based, reusable components that implement many common data structures and algorithms used to process those data structures. The STL offers proof of concept for generic programming with templatesintroduced in Chapter and demonstrated in detail in Chapter . This chapter introduces the STL and discusses its three key components containers (popular templatized data structures), iterators and algorithms. The STL containers are data structures capable of storing objects of any data type. We will see that there are three container categoriesfirst-class containers, adapters and near containers. STL iterators, which have similar properties to pointers, are used by programs to manipulate the STL-container elements. In fact, standard arrays can be manipulated as STL containers, using standard pointers as iterators. We will see that manipulating containers with iterators is convenient and provides tremendous expressive power when combined with STL algorithmsin some cases, reducing many lines of code to a single statement. STL algorithms are functions that perform common data manipulations such as searching, sorting, comparing elements (or entire data structures), etc. There are approximately 70 algorithms implemented in the STL. Most of these algorithms use iterators to access container elements. We will see that each first-class container supports specific iterator types, some of which are more powerful than others. A container's supported iterator type determines whether the container can be used with a specific algorithm. Iterators encapsulate the mechanism used to access container elements. This encapsulation enables many of the STL algorithms to be applied to several containers without regard for the underlying container implementation. As long as a container's iterators support the minimum requirements of the algorithm, then the algorithm can process that container's elements. This also enables programmers to create algorithms that can process the elements of multiple different container types. Chapter discusses how to implement data structures with pointers, classes and dynamic memory. Pointer-based code is complex, and the slightest omission or oversight can lead to serious memory-access violations and memory-leak errors with no compiler complaints. Implementing additional data structures such as deques, priority queues, sets, maps, etc. requires substantial additional work. In addition, if many programmers on a large project implement similar containers and algorithms for different tasks, the code becomes difficult to modify, maintain and debug. An advantage of the STL is that programmers can reuse the STL containers, iterators and algorithms to implement common data representations and manipulations. This reuse results in substantial development-time and resource savings. This chapter is meant to be an introduction to the STL. It is neither complete nor comprehensive. However, it is a friendly, accessible chapter that should convince you of the value of the STL and encourage further study. This might be one of the most important chapters in the book in terms of your appreciation of software reuse.
Chapter 22 Other Topics
is a collection of miscellaneous C++ topics. This chapter discusses two cast operatorsconst_cast and reinterpret_cast. These operators, along with static_cast (Chapter ) and dynamic_cast (Chapter ), provide a more robust mechanism for converting between types than do the original cast operators C++ inherited from C. We discuss namespaces, a feature particularly crucial for software developers who build substantial systems, especially for those who build systems from class libraries. Namespaces prevent naming collisions, which can hinder such large software efforts. We discuss the operator keywords, which are useful for programmers who do not like cryptic operator symbols. The primary use of these symbols is in international markets, where certain characters are not always available on local keyboards. We discuss keyword explicit, which prevents the compiler from invoking conversion constructors in undesirable situations; explicit-conversion constructors can be invoked only through constructor syntax, not through implicit conversions. We discuss keyword mutable, which allows a member of a const object to be changed. Previously, this was accomplished by "casting away const-ness", which is considered a dangerous practice. We also discuss pointer-to-member operators .* and ->*, multiple inheritance (including the problem of "diamond inheritance") and virtual base classes.