The C++ Standard Template Library / Edition 1

Paperback (Print)
Buy New
Buy New from BN.com
$52.07
Used and New from Other Sellers
Used and New from Other Sellers
from $1.99
Usually ships in 1-2 business days
(Save 96%)
Other sellers (Paperback)
  • All (15) from $1.99   
  • New (7) from $50.90   
  • Used (8) from $1.99   

Overview

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.

Standard Template Libraries (STL) were created to provide C++ programmers with a suite of rusable 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.

Read More Show Less

Editorial Reviews

Booknews
Defines the template classes and functions of the standard template library (STL) component of the C++ programming language. A chapter is devoted to each of the 13 headers, providing a functional description of the header contents, suggestions for how best to use the facilities defined in the header, and the C++ code itself. Additional chapters introduce STL as a whole and discuss three overarching topics<-- >iterators, algorithms, and containers. Annotation c. Book News, Inc., Portland, OR (booknews.com)
Read More Show Less

Product Details

  • ISBN-13: 9780134376332
  • Publisher: Prentice Hall
  • Publication date: 12/11/2000
  • Edition description: New Edition
  • Edition number: 1
  • Pages: 512
  • Product dimensions: 7.00 (w) x 9.00 (h) x 1.20 (d)

Meet 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.

Read More Show Less

Read an Excerpt

Chapter 11: <list>

Background

The header <list> defines just the template class list, which is a container that stores its controlled sequence of length N as a bidirectional linked list of N nodes, each of which stores a single element. The advantage of a linked list is its flexibility. You can insert and remove elements freely and easily within the list, just by rewriting the forward and backward links in nodes. You can even splice in whole sublists. The list nodes themselves don’t move about in memory. As a consequence any iterators you maintain to designate individual nodes remain validfor thelifeofthe node.Similarly, any pointers you maintain to the individual list element itself also remain valid for the life of the node in which the element resides.

The price you pay is sequential access to arbitrary elements in the sequence. To access element number i, for example, you have to chain from one node to another i times, beginning with a pointer to the head of the list stored in the container object. You can chain in either direction, but chain you must. So the mean time to locate an arbitrary element increases linearly with the total number of elements in the controlled sequence. Using STL terminology, template class list supports bidirectional iterators.

Table 11.1, on page 240, shows how template class list stacks up against the other STL containers. It is the clear winner for all operations that rearrange list elements (insertions, erasures, and replacements). It is the clear loser for all operations that locate arbitrary elements (searches and random access). It also requires a moderately hefty overhead of two point-ers per element, the forward and backward links stored in each node.

Template class list defines several member functions that take advan- tage of its peculiar properties. For example, you can splice elements from one list into another, sort a list, or merge one ordered list into another. All these operations simply restitch links between list nodes. No copying occurs. The payoff can be significant for a list of elements that are expensive to copy — because they are large or have nontrivial copy semantics.

Template class list has an additional virtue. It alone of the template containers promises to behave predictably in the presence of exceptions thrown by programmer-supplied code. Other containers provide a weaker guarantee. (See Chapter 9: Containers.) For any container, an exception thrown during execution of a member function leaves the container in a consistent state, suitable for destruction; and the container does not lose track of allocated storage. But for many operations, particularly those that affect multiple elements, the exact state of the container is unspecified when the exception is rethrown. list, by contrast, guarantees for most member functions that any interrupted member function call leaves the container in its original state when it rethrows the exception.

So in summary, you use template class list when you need flexibility in rearranging sequences of elements, and in keeping track of individual elements by storing iterators that remain valid across rearrangements. You also use template class list when you need greater determinism in the presence of exceptions. On the other hand, locating arbitrary elements within a list object is relatively expensive, even if the list is kept in order, since you have to perform a linear search each time. Consider other containers if more rapid access is important.

Functional Description

namespace std {
template<class T, class A>
class list;
// TEMPLATE FUNCTIONS
template<class T, class A>
bool operator==(
const list<T, A>& lhs,
const list<T, A>& rhs);
template<class T, class A>
bool operator!=(
const list<T, A>& lhs,
const list<T, A>& rhs);
template<class T, class A>
bool operator<(
const list<T, A>& lhs,
const list<T, A>& rhs);
template<class T, class A>
bool operator>(
const list<T, A>& lhs,
const list<T, A>& rhs);
template<class T, class A>
bool operator<=(
const list<T, A>& lhs,
const list<T, A>& rhs);
template<class T, class A>
bool operator>=(
const list<T, A>& lhs,
const list<T, A>& rhs);
template<class T, class A>
void swap(
list<T, A>& lhs,
list<T, A>& rhs);
};

Include the STL standard header <list> to define the container template class list and several supporting templates.

template<class T, class A = allocator<T> >
class list {
public:
typedef A allocator_type;
typedef typename A::pointer pointer;
typedef typename A::const_pointer
const_pointer;
typedef typename A::reference reference;
typedef typename A::const_reference const_reference;
typedef typename A::value_type value_type;
typedef T0 iterator;
typedef T1 const_iterator;
typedef T2 size_type;
typedef T3 difference_type;
typedef reverse_iterator<const_iterator>
const_reverse_iterator;
typedef reverse_iterator<iterator>
reverse_iterator;
list();
explicit list(const A& al);
explicit list(size_type n);
list(size_type n, const T& v);
list(size_type n, const T& v, const A& al);
list(const list& x);
template<class InIt>
list(InIt first, InIt last);
template<class InIt>
list(InIt first, InIt last, const A& al);
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;
void resize(size_type n);
void resize(size_type n, T x);
size_type size() const;
size_type max_size() const;
bool empty() const;
A get_allocator() const;
reference front();
const_reference front() const;
reference back();
const_reference back() const;
void push_front(const T& x);
void pop_front();
void push_back(const T& x);
void pop_back();
template<class InIt>
void assign(InIt first, InIt last);
void assign(size_type n, const T& x);
iterator insert(iterator it, const T& x);
void insert(iterator it, size_type n, const T& x);
template<class InIt>
void insert(iterator it, InIt first, InIt last);
iterator erase(iterator it);
iterator erase(iterator first, iterator last);
void clear();
void swap(list& x);
void splice(iterator it, list& x);
void splice(iterator it, list& x, iterator first);
void splice(iterator it, list& x, iterator first,
iterator last);
void remove(const T& x);
templace<class Pred>
void remove_if(Pred pr);
void unique();
template<class Pred>
void unique(Pred pr);
void merge(list& x);
template<class Pred>
void merge(list& x, Pred pr);
void sort();
template<class Pred>
void sort(Pred pr);
void reverse();
};

The template class describes an object that controls a varying-length sequence of elements of type T. The sequence is stored as a bidirectional linked list of elements, each containing a member of type T....

Read More Show Less

Table of Contents

Prefaceix
The Codexi
Acknowledgmentsxii
Chapter 0Introduction1
Background1
Functional Description7
Using STL9
Implementing STL10
Testing STL15
Exercises16
Chapter 1Iterators17
Background17
Functional Description17
Using Iterators25
Exercises26
Chapter 227
Background27
Functional Description29
Using 31
Implementing 33
Testing 33
Exercises35
Chapter 337
Background37
Functional Description48
Using 65
Implementing 70
Testing 89
Exercises94
Chapter 497
Background97
Functional Description106
Using 115
Implementing 117
Testing 126
Exercise129
Chapter 5Algorithms131
Background131
Functional Description133
Using Algorithms134
Exercises135
Chapter 6137
Background137
Functional Description137
Using 163
Implementing 167
Testing 206
Exercises207
Chapter 7209
Background209
Functional Description209
Using 211
Implementing 211
Testing 211
Exercises213
Chapter 8215
Background215
Functional Description217
Using 226
Implementing 235
Testing 235
Exercises238
Chapter 9Containers239
Background239
Functional Description241
Using Containers247
Exercises249
Chapter 10251
Background251
Functional Description253
Using 263
Implementing 266
Testing 285
Exercises287
Chapter 11289
Background289
Functional Description290
Using 300
Implementing 303
Testing 318
Exercises320
Chapter 12323
Background323
Functional Description324
Using 333
Implementing 335
Testing 352
Exercises352
Chapter 13353
Background353
Functional Description359
Using 375
Implementing 377
Testing 398
Exercises398
Chapter 14403
Background403
Functional Description404
Using 421
Implementing 424
Testing 427
Exercises427
Chapter 15433
Background433
Functional Description434
Using 437
Implementing 438
Testing 440
Exercises440
Chapter 16443
Background443
Functional Description444
Using 449
Implementing 452
Testing 454
Exercises456
Appendix AInterfaces457
Appendix BTerms461
Appendix CReferences473
Index475
Read More Show Less

Preface

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.

Read More Show Less

Customer Reviews

Be the first to write a review
( 0 )
Rating Distribution

5 Star

(0)

4 Star

(0)

3 Star

(0)

2 Star

(0)

1 Star

(0)

Your Rating:

Your Name: Create a Pen Name or

Barnes & Noble.com Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & Noble.com that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & Noble.com does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at BN.com or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation

Reminder:

  • - By submitting a review, you grant to Barnes & Noble.com and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Noble.com Terms of Use.
  • - Barnes & Noble.com reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & Noble.com also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on BN.com. It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

 
Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously

    If you find inappropriate content, please report it to Barnes & Noble
    Why is this product inappropriate?
    Comments (optional)