Your browser does not support JavaScript and this site utilizes JavaScript to build content and provide links to additional information. You should either enable JavaScript in your browser settings or use a browser that supports JavaScript in order to take full advantage of this site.

The Standard Template Library Tutorial

by P.J. Plauger, Alexander A. Stepanov, Meng Lee, and David R. Musser

Download Book(Respecting the intellectual property of others is utmost important to us, we make every effort to make sure we only link to legitimate sites, such as those sites owned by authors and publishers. If you have any questions about these links, please contact us.)

Amazon.com
Written by its inventors, The C++ Standard Template Library is a must-have for any serious intermediate or advanced C++ developer. Containing a full reference to all available Standard Template Library (STL) features and filled with expert advice, this book will give you the means to use this powerful library more effectively.

As most C++ developers know, the STL bundles robust container classes and nearly 100 algorithms (used to efficiently search, sort, and manipulate data). However, tapping its power can be a challenge. Many books on STL resort to the arcane syntax of templates and can be couched in the language of computer science. The C++ Standard Template Library breaks this mold with a clearly presented tour of STL from top to bottom, based on the individual header files that make up this library. Because it was written by its original inventors, you get a true insider's perspective.

Starting with iterators (used to navigate through data) and an in-depth guide to STL algorithms, you'll learn the right way to use STL from the ground up, including specific features and APIs. Each chapter is organized so that newcomers can learn the basics first, with a reference and guide to APIs and how to use them. The nitty-gritty details follow. (Each chapter includes the full source code from Hewlett-Packard's implementation of each STL header file, along with annotations and suggested programming exercises to try out on your own.) Besides full source code, expert readers will benefit from the description of the design choices made by the STL author/inventors, as well as tips for performance.

The book closes with a full tour of STL containers (including the vector, list, set, and map classes), plus hints for selecting the right containers based on your programming needs. (It helps that the discussion on containers occurs after the material on algorithms, making STL containers even more flexible.) In all, The C++ Standard Template Library looks to be an important book, one that will help anyone with some C++ experience get productive with STL. --Richard Dragan

This text was created to provide C++ programmers with a suite of reusable programs, or lines of code, that could be used by everyone to increase programming productivity and quality. Every C++ programmer will need at least one off-the-shelf STL reference guide. Each chapter covers one STL component, and includes background, a review of the standard, using the component, implementing the component, and exercises.

Book Info
(Prentice Hall PTR) A thorough explanation of the C++ Standard Template Library from its creators. Each chapter covers one STL component, detailing for each the background of the component and its uses, extensive sample code, hands-on exercises, techniques for using and implementing the component, and a review of the relevant standards. Softcover. DLC: C++ (Computer program language).

The publisher, Prentice-Hall ECS Professional
Standard Template Libraries (STL) were created to provide C++ programmers with a suite of reusable programs, or lines of code, that could be used by everyone to increase programming productivity and quality. This book is the definitive reference on C++ programming using STL, as it was written by the team that created the library. Every C++ programmer will need at least one off-the-shelf STL reference guide. Each chapter covers one STL component, and includes background, a review of the standard, using the component, implementing the component, and exercises. For C++ Software Development Managers and C++ programmers at all levels.

From the Back Cover

With the ANSI/ISO C++ Standard Template Library (STL), C++ developers have a powerful toolset for maximizing productivity, software quality, and performance at the same time. STL provides both comprehensive sets of container classes and fundamental algorithms to go with them-a large, systematic, and efficient framework for C++ development. Now, in this long-awaited book, the creators of the C++ Standard Template Library explain it authoritatively and in depth. Each chapter of The C++ Standard Template Library covers one STL component, including:

Detailed background about the component and its uses

A thorough review of the relevant ANSI/ISO C++ standard

Practical techniques for using and implementing the component

Extensive sample code

Hands-on exercises

Appendices present lists of reserved names, a detailed glossary of terms and references, and how STL interfaces with the rest of the Standard C++ library.

Fully reflective of the final ANSI/ISO STL standard, The C++ Standard Template Library is the last word on all aspects of STL development for every experienced C++ programmer.

About the Author

P.J. Plauger is President of Dinkumware, Ltd., which licenses standards-conforming C and C++ libraries and documentation. For many years, he served as Senior Editor of The C/C++ Users Journal and Contributing Editor to Embedded Systems Programming. Plauger has long been active in the development of C and C++ international standards.

Alex Stepanov, now at AT&T, formerly headed the Generic Programming Project at Hewlett-Packard Research Laboratories in Palo Alto, California.

Meng Lee is currently a Technical Contributor at Hewlett-Packard Research Labs where she and Stepanov developed the original STL. The STL was proposed to and accepted by the ANCI/ISO C++ Standards Committee.

David R. Musser, Professor of Computer Science at Rensselaer Polytechnic Institute, specializes in research on generic programming. His work with Stepanov provided the foundation for the STL.

Preface

The Standard Template Library (or STL for short) is a major component of the library portion of the ANSI/ISO Standard for the programming language C++. It was developed at Hewlett-Packard Labs by Alexander Stepanov and Meng Lee (S&L95), based heavily on earlier work by Stepanov and David R. Musser of Rensselaer Polytechnic Institute. (See M&S87, M&S89, and M&S94. All citations are listed in Appendix C: References.) You will find this software package to be a most ambitious, and coherent, use of templates in C++. Indeed, STL has already begun to alter significantly the way many programmers write C++ code.

This book shows you how to use the template classes and functions of STL as mandated by the C++ Standard (clauses 20 and 23-26). We focus here on STL as a fairly self-contained subset of a larger library defined for Standard C++. That library is designed to work, in turn, with the library defined for ANSI/ISO Standard C (ANS89 and ISO90). Consequently, you can look on this book as a companion to two earlier works by P.J. Plauger, The Standard C Library (Pla92) and The Draft Standard C++ Library (Pla95). Taken together, these three books describe nearly all the library facilities of widespread interest to C++ programmers.

C++ Standard

The C++ Standard was formally approved in 1998 and should be stable for years to come. It is both an ANSI Standard (within the USA) and an ISO Standard (around the world). As part of the standardization process, the entire Standard C++ language and library were for the first time described completely and in one place. A relatively late event in that process was the incorporation of STL as part of the draft C++ Standard. More recent still is the wider dissemination of various implementations of Standard C++ compilers and libraries. Thus, much of what you will find in this book is news, even to experienced C++ programmers.

Similarly, the description of STL (precursors to S&L95) saw only limited circulation before its incorporation into the draft C++ Standard of July 1994. In the process of incorporation, it has been reorganized and changed in several significant ways. The STL in Standard C++ is not the package made widely available by Hewlett-Packard. It is not the version more recently enhanced by many different agencies. Thus, even programmers with early experience using STL can profit from this book. You will find here a complete presentation of STL as it is specified by the C++ Standard.

The book also shows you at least one way to implement STL. We present about 6,000 lines of tested, working code that is known to be portable across a number of C++ compilers. It is, in fact, essentially the same code that you will find distributed with Microsoft Visual C++, IBM Visual Age, as well as with C++ compilers offered by a number of other vendors. Minor changes have been made in formatting and notation, to make the book form of the code more readable and tutorial.

To a large extent, the code presented here works atop any C++ library. (See Appendix A: Interfaces.) It is designed to work particularly well, however, with a library that conforms closely to the C++ Standard. As much as possible, the code avoids forms that are non portable or not likely to be widely available. Code that relies on recent additions to the C++ language, such as template partial specialization, may still present problems with some compilers. You will find that commercial versions of this library indulge in various compromises to deal with such dialect issues.

One way or another, you can use the code presented here to gain valuable experience using a library of templates that is well on its way to becoming an important standard for the C++ world. Equally important, we believe that seeing a realistic implementation of STL can help you better under-stand how to use it.

And that introduces yet another goal. Besides presenting STL as standardized, and working code to implement it, this book also serves as a tutorial on how to use this library. You will find here useful background information on how the library came to take its present form, how it was meant to be used, and how it should be used. You don't have to read and understand all the code presented here to achieve that basic goal. Even a cursory study is beneficial. You certainly don't have to be a sophisticated user to profit from this book, but the programmer who is just an occasional sophisticate will find the information presented here invaluable.

Teaching you how to write C++ is not a goal of this book. We assume you know enough about C++ to read straightforward code. Where the code presented is not so straightforward, we explain the trickery involved.

extending STL

A final purpose of this book is to teach programmers how to design and implement extensions to the Standard Template Library. STL brings to-gether numerous algorithms, data structures, and programming tech-niques. It is not, however, intended to supply a complete set of facilities that a programmer might need. Rather, it provides a core of the most widely used facilities. And it describes the rules by which this core code is written. Once you learn to follow these rules, you can add your own algorithms that work with existing data structures. You can also add data structures that work with existing algorithms. Using the STL code presented here as an example, you will soon learn how to tackle new problems by writing a minimum of new code. And you will find that the new code is itself often reusable on future projects. That's what library design is all about, in the end.

structure of this book

The book is structured much like the STL code itself. The C++ Standard specifies numerous headers, but only thirteen of these headers define all the templates in STL. A separate chapter covers each header. Additional chapters introduce STL as a whole and discuss three overarching topics  iterators, algorithms, and containers. Most of the headers have reasonably cohesive contents. That makes for reasonably cohesive discussions. One or two, however, are catchalls. Their corresponding chapters are perforce wider ranging.

Each chapter that describes a header follows the same pattern. It begins with a brief background section, followed by a functional description of the header contents, then by suggestions for how best to use the facilities defined in the header. We then present the C++ code that constitutes the header itself, accompanied by a commentary on how the code works. We also present a small test program for each header, to provide at least a cursory example of how each template definition might be used.

Each chapter ends with a set of exercises. In a university course based on this book, the exercises can serve as homework problems. Many of them are simple exercises in using the library, or in code rewriting. They drive home a point or illustrate reasonable variations in implementation. The more ambitious exercises are labeled as such. They can serve as a basis for more extended projects. The independent reader can use the exercises as stimulus for further thought.

The Code

The code presented in this book and its description are based on the version of STL made widely available by Hewlett-Packard Company. That version contains the following notice:

Copyright (c) 1994 by Hewlett-Packard Company Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Hewlett-Packard Company makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.

The code in this book has been tested, to various degrees, with C++ compilers from Microsoft, IBM, Edison Design Group, and Project GNU. It passes all the STL tests in the Dinkum C++ Proofer and the Dinkum Abridged Proofer, both quality-assurance tools for the corresponding libraries available commercially from Dinkumware, Ltd. It has also passed various versions of the commercial library validation suites available from Perennial Software and Plum Hall Inc. While we have taken pains to minimize errors, we cannot guarantee that none remain. Please note carefully the disclaimer on the copyright page.

Please note also that the code in this book is protected by copyright. It has not been placed in the public domain. Nor is it shareware. It is not protected by a "copy left" agreement, like code distributed by the Free Software Foundation (Project GNU). P.J. Plauger retains all rights. Dinkumware, Ltd. licenses it on a commercial basis.

The STL code that comes with a given C++ compiler may differ from the code presented here in various ways. This is true even of code based on that presented here. Dialects of C++ still abound, particularly in the complex area of template processing. Over time, all these dialects should converge, thanks to the completion of the C++ Standard. But for some time to come, you should consider the code presented here as reasonably representative of many implementations.

Comments

SEND A COMMENT

PLEASE READ: All comments must be approved before appearing in the thread; time and space constraints prevent all comments from appearing. We will only approve comments that are directly related to the article, use appropriate language and are not attacking the comments of others.