did-you-know? rent-now

Amazon no longer offers textbook rentals. We do!

did-you-know? rent-now

Amazon no longer offers textbook rentals. We do!

We're the #1 textbook rental company. Let us show you why.

9780201379266

The C++ Standard Library A Tutorial and Reference

by
  • ISBN13:

    9780201379266

  • ISBN10:

    0201379260

  • Edition: 1st
  • Format: Hardcover
  • Copyright: 1999-08-12
  • Publisher: Addison-Wesley Professional
  • View Upgraded Edition

Note: Supplemental materials are not guaranteed with Rental or Used book purchases.

Purchase Benefits

  • Free Shipping Icon Free Shipping On Orders Over $35!
    Your order must be $35 or more to qualify for free economy shipping. Bulk sales, PO's, Marketplace items, eBooks and apparel do not qualify for this offer.
  • eCampus.com Logo Get Rewarded for Ordering Your Textbooks! Enroll Now
List Price: $79.99 Save up to $20.00
  • Buy Used
    $59.99
    Add to Cart Free Shipping Icon Free Shipping

    USUALLY SHIPS IN 2-4 BUSINESS DAYS

Supplemental Materials

What is included with this book?

Summary

In the beginning, I only planned to write a small German book (400 pages or so) about the C++ Standard Library. That was in 1993. Now, in 1999 you see the result--an English book with more than 800 pages of facts, figures, and examples. My goal is to describe the C++ Standard Library so that all (or almost all) your programming questions are answered before you think of the question. Note, however, that this is not a complete description of all aspects of the C++ Standard Library. Instead, I present the most important topics necessary for learning and programming in C++by using its standard library.

Each topic is described based on the general concepts;this discussion then leads to the specific details needed to support everyday programming tasks. Specific code examples are provided to help you understand the concepts and the details.

That's it--in a nutshell. I hope you get as much pleasure from reading this book as I did from writing it. Enjoy!

Acknowledgments

This book presents ideas, concepts,solutions, and examples from many sources. In a way it does not seem fair that my name is the only name on the cover. Thus, I'd like to thank all the people and companies who helped and supported me during the past few years.

First, I'd like to thank Dietmar Kuhl. Dietmar is an expert on C++, especially on input/output streams and internationalization (he implemented an I/O stream library just for fun). He not only translated major parts of this book from German to English, he also wrote sections of this book using his expertise. In addition, he provided me with invaluable feedback over the years.

Second, I'd like to thank all the reviewers and everyone else who gave me their opinion. These people endow the book with a quality it would never have had without their input. (Because the list is extensive, please forgive me for any oversight.) The reviewers for the English version of this book included Chuck Allison, Greg Comeau, James A. Crotinger, Gabriel Dos Reis, Alan Ezust, Nathan Meyers, Werner Mossner, Todd Veldhuizen, Chichiang Wan, Judy Ward, and Thomas Wikehult. The German reviewers included Ralf Boecker, Dirk Herrmann, Dietmar Kuhl, Edda Lorke, Herbert Scheubner, Dominik Strasser, and Martin Weitzel. Additional input was provided by Matt Austern, Valentin Bonnard, Greg Colvin, Beman Dawes, Bill Gibbons, Lois Goldthwaite, Andrew Koenig, Steve Rumbsby, Bjarne Stroustrup, and David Vandevoorde.

Special thanks to Dave Abrahams, Janet Cocker, Catherine Ohala, and Maureen Willard who reviewed and edited the whole book very carefully. Their feedback was an incredible contribution to the quality of this book.

A special thanks goes to my "personal living dictionary"--Herb Sutter--the author of the famous "Guru of the Week" (a regular series of C++ programming problems that is published on the comp.std.c++.moderated Internet newsgroup).

I'd also like to thank all the people and companies who gave me the opportunity to test my examples on different platforms with different compilers. Many thanks to Steve Adamczyk, Mike Anderson, and John Spicer from EDG for their great compiler and their support. It was a big help during the standardization process and the writing of this book. Many thanks to P. J. Plauger and Dinkumware, Ltd, for their early standard-conforming implementation of the C++ Standard Library. Many thanks to Andreas Hommel and Metrowerks for an evaluative version of their CodeWarrior Programming Environment. Many thanks to all the developers of the free GNU and egcs compilers. Many thanks to Microsoft for an evaluative version of Visual C++. Many thanks to Roland Hartinger from Siemens Nixdorf Informations Systems AG for a test version of their C++ compiler. Many thanks to Topjects GmbH for an evaluative version of the ObjectSpace library implementation.

And, of course many thanks for those who invented, designed, or implemented C++ or parts of the library. Among others are Bjarne Stroustrup, Alexander Stepanov, Meng Lee, Matt Austern, Boris P. Fomichev, and all the other guys who wrote the SGI STL and the STLport.

Many thanks to everyone from Addison Wesley Longman who worked with me. Among others this includes Janet Cocker, Mike Hendrickson, Debbie Lafferty, Marina Lang, Chanda Leary, Catherine Ohala, Marty Rabinowitz, Susanne Spitzer,and Maureen Willard. It was fun.

In addition, I'd like to thank the people at BREDEX GmbH and all the people in the C++ community, particularly those involved with the standardization process, for their support and patience (sometimes I ask really silly questions).

Last but not least, many thanks and kisses for my family: Ulli, Lucas, Anica, and Frederic. I definitely did not have enough time for them due to the writing of this book.

Have fun and be human!

About this Book

Soon after its introduction, C++ became a de facto standard in object-oriented programming. This led to the goal of standardization. Only by having a standard, could programs be written that would run on different platforms--from PCs to mainframes. Furthermore, a standard C++ library would enable programmers to use general components and a higher level of abstraction without losing portability, rather than having to develop all code from scratch.

The standardization process was started in 1989 by an international ANSI/ISO committee. It developed the standard based on Bjarne Stroustrup's books ( The C++ Programming Language ) and ( The Annotated C++ Reference Manual) . After the standard was completed in 1997,several formal motions by different countries made it an international ISO and ANSI standard in 1998. The standardization process included the development of a C++ Standard Library. The library extends the core language to provide some general components. By using C++'s ability to program new abstract and generic types, the library provides a set of common classes and interfaces. This gives programmers a higher level of abstraction. The library provides the ability to use string types, different data structures (such as dynamic arrays, linked lists, and binary trees), different algorithms (such as different sorting algorithms), numeric classes, input/output (I/O) classes, and classes for internationalization support.

All of these are supported by a fairly simple programming interface. These components are very important for many programs. These days, data processing often means inputting, computing, processing, and outputting large amounts of data, which are often strings.

The library is not self-explanatory. To use these components and to benefit from their power,you need a good introduction that explains the concepts and the important details instead of simply listing the classes and their functions. This book is written exactly for that purpose. First, it introduces the library and all of its components from a conceptional point of view. Next, it describes the details needed for practical programming. Examples are included to demonstrate the exact usage of the components. Thus, this book is a detailed introduction to the C++ library for both the beginner and the practical programmer. Armed with the data provided herein, you should be able to take full advantage of the C++ Standard Library.

Caveat

I don't promise that everything described is easy and self-explanatory. The library provides a lot of flexibility,but flexibility for nontrivial purposes has a price. Beware that the library has traps and pitfalls,which I point out when we encounter them and suggest ways of avoiding them.

What You Should Know Before Reading this Book

To get the most from

Author Biography

Nicolai M. Josuttis is an independent technical consultant who designs object-oriented software for the telecommunication, traffic, finance, and manufacturing industries. He is an active member of the C++ Standard Committee library working group and a partner at System Bauhaus, a German group of recognized object-oriented system development experts. Josuttis has written several books on object-oriented programming and C++, including Die C++-Standardbibliothek and Objektorientiertes Programmieren in C++.

Table of Contents

Preface xvii
Acknowledgments xix
About this Book
1(6)
Why this Book
1(1)
What You Should Know Before Reading this Book
2(1)
Style and Structure of the Book
2(2)
How to Read this Book
4(1)
State of the Art
5(1)
Example Code and Additional Information
5(1)
Feedback
5(2)
Introduction to C++ and the Standard Library
7(16)
History
7(2)
New Language Features
9(12)
Templates
9(5)
Explicit Initialization for Fundamental Types
14(1)
Exception Handing
15(1)
Namespaces
16(2)
Type bool
18(1)
Keyword explicit
18(1)
New Operators for Type Conversion
19(1)
Initialization of Constant Static Members
20(1)
Definition of main ()
21(1)
Complexity and the Big-O Notation
21(2)
General Concepts
23(10)
Namespace std
23(1)
Header Files
24(1)
Error and Exception Handling
25(6)
Standard Exception Classes
25(3)
Members of Exception Classes
28(1)
Throwing Standard Exceptions
29(1)
Deriving Standard Exception Classes
30(1)
Allocators
31(2)
Utilities
33(40)
Pairs
33(5)
Convenience Function make_pair()
36(1)
Examples of Pair Usage
37(1)
Class auto_ptr
38(21)
Motivation of Class auto_ptr
38(2)
Transfer of Ownership by auto_ptr
40(4)
auto_ptrs as Members
44(2)
Misusing auto_ptrs
46(1)
auto_ptr Examples
47(4)
Class auto_ptr in Detail
51(8)
Numeric Limits
59(7)
Auxiliary Functions
66(3)
Processing the Minimum and Maximum
66(1)
Swapping Two Values
67(2)
Supplementary Comparison Operators
69(2)
Header Files <cstddef> and <cstdlib>
71(2)
Definitions in <cstddef>
71(1)
Definitions in <cstdlib>
71(2)
The Standard Template Library
73(70)
STL Components
73(2)
Containers
75(8)
Sequence Containers
76(5)
Associative Containers
81(1)
Container Adapters
82(1)
Iterators
83(11)
Examples of Using Associative Containers
86(7)
Iterator Categories
93(1)
Algorithms
94(10)
Ranges
97(4)
Handling Multiple Ranges
101(3)
Iterator Adapters
104(7)
Insert Iterators
104(3)
Stream Iterators
107(2)
Reverse Iterators
109(2)
Manipulating Algorithms
111(6)
``Removing'' Elements
111(4)
Manipulating Algorithms and Associative Containers
115(1)
Algorithms versus Member Functions
116(1)
User-Defined Generic Functions
117(2)
Functions as Algorithm Arguments
119(5)
Examples of Using Functions as Algorithm Arguments
119(2)
Predicates
121(3)
Function Objects
124(10)
What Are Function Objects?
124(7)
Predefined Function Objects
131(3)
Container Elements
134(2)
Requirements for Container Elements
134(1)
Value Semantics or Reference Semantics
135(1)
Errors and Exceptions Inside the STL
136(5)
Error Handling
137(2)
Exception Handling
139(2)
Extending the STL
141(2)
STL Containers
143(108)
Common Container Abilities and Operations
144(4)
Common Container Abilities
144(1)
Common Container Operations
144(4)
Vectors
148(12)
Abilities of Vectors
148(2)
Vector Operations
150(5)
Using Vectors as Ordinary Arrays
155(1)
Exception Handling
155(1)
Examples of Using Vectors
156(2)
Class vector <bool>
158(2)
Deques
160(6)
Abilities of Deques
161(1)
Deque Operations
162(2)
Exception Handling
164(1)
Examples of Using Deques
164(2)
Lists
166(9)
Abilities of Lists
166(1)
List Operations
167(5)
Exception Handling
172(1)
Examples of Using Lists
172(3)
Sets and Multisets
175(19)
Abilities of Sets and Multisets
176(1)
Set and Multiset Operations
177(8)
Exception Handling
185(1)
Examples of Using Sets and Multisets
186(5)
Example of Specifying the Sorting Criterion at Runtime
191(3)
Maps and Multimaps
194(23)
Abilities of Maps and Multimaps
195(1)
Map and Multimap Operations
196(9)
Using Maps as Associative Arrays
205(2)
Exception Handling
207(1)
Examples of Using Maps and Multimaps
207(6)
Example with Maps, Strings, and Sorting Criterion at Runtime
213(4)
Other STL Containers
217(5)
Strings as STL Containers
217(1)
Ordinary Arrays as STL Containers
218(3)
Hash Tables
221(1)
Implementing Reference Semantics
222(4)
When to Use which Container
226(4)
Container Types and Members in Detail
230(21)
Type Definitions
230(1)
Create, Copy, and Destroy Operations
231(2)
Nonmodifying Operations
233(3)
Assignments
236(1)
Direct Element Access
237(2)
Operations to Generate Iterators
239(1)
Inserting and Removing Elements
240(4)
Special Member Functions for Lists
244(2)
Allocator Support
246(2)
Overview of Exception Handling in STL Containers
248(3)
STL Iterators
251(42)
Header Files for Iterators
251(1)
Iterator Categories
251(8)
Input Iterators
252(1)
Output Iterators
253(1)
Forward Iterators
254(1)
Bidirectional Iterators
255(1)
Random Access Iterators
255(3)
The Increment and Decrement Problem of Vector Iterators
258(1)
Auxiliary Iterator Functions
259(5)
Stepping Iterators Using advance()
259(2)
Processing Iterator Distance Using distance()
261(2)
Swapping Iterator Values Using iter_swap()
263(1)
Iterator Adapters
264(19)
Reverse Iterators
264(7)
Insert Iterators
271(6)
Stream Iterators
277(6)
Iterator Traits
283(10)
Writing Generic Functions for Iterators
285(3)
User-Defined Iterators
288(5)
STL Function Objects
293(28)
The Concept of Function Objects
293(12)
Function Objects as Sorting Criteria
294(2)
Function Objects with Internal State
296(4)
The Return Value of for_each()
300(2)
Predicates versus Function Objects
302(3)
Predefined Function Objects
305(8)
Function Adapters
306(1)
Function Adapters for Member Functions
307(2)
Function Adapters for Ordinary Functions
309(1)
User-Defined Function Objects for Function Adapters
310(3)
Supplementary Composing Function Objects
313(8)
Unary Compose Function Object Adapters
314(4)
Binary Compose Function Object Adapters
318(3)
STL Algorithms
321(114)
Algorithm Header Files
321(1)
Algorithm Overview
322(10)
A Brief Introduction
322(1)
Classification of Algorithms
323(9)
Auxiliary Functions
332(2)
The for_each() Algorithm
334(4)
Nonmodifying Algorithms
338(25)
Counting Elements
338(1)
Minimum and Maximum
339(2)
Searching Elements
341(15)
Comparing Ranges
356(7)
Modifying Algorithms
363(15)
Copying Elements
363(3)
Transforming and Combining Elements
366(4)
Swapping Elements
370(2)
Assigning New Values
372(3)
Replacing Elements
375(3)
Removing Algorithms
378(8)
Removing Certain Values
378(3)
Removing Duplicates
381(5)
Mutating Algorithms
386(11)
Reversing the Order of Elements
386(2)
Rotating Elements
388(3)
Permuting Elements
391(2)
Shuffling Elements
393(2)
Moving Elements to the Front
395(2)
Sorting Algorithms
397(12)
Sorting All Elements
397(3)
Partial Sorting
400(4)
Sorting According to the nth Element
404(2)
Heap Algorithms
406(3)
Sorted Range Algorithms
409(16)
Searching Elements
410(6)
Merging Elements
416(9)
Numeric Algorithms
425(10)
Processing Results
425(4)
Converting Relative and Absolute Values
429(6)
Special Containers
435(36)
Stacks
435(9)
The Core Interface
436(1)
Example of Using Stacks
437(1)
Class stack<> in Detail
438(3)
A User-Defined Stack Class
441(3)
Queues
444(9)
The Core Interface
445(1)
Example of Using Queues
446(1)
Class queue<> in Detail
447(3)
A User-Defined Queue Class
450(3)
Priority Queues
453(7)
The Core Interface
455(1)
Example of Using Priority Queues
455(1)
Class priority_queue<> in Detail
456(4)
Bitsets
460(11)
Examples of Using Bitsets
460(3)
Class bitset in Detail
463(8)
Strings
471(58)
Motivation
471(8)
A First Example: Extracting a Temporary File Name
472(4)
A Second Example: Extracting Words and Printing Them Backward
476(3)
Description of the String Classes
479(28)
String Types
479(2)
Operation Overview
481(2)
Constructors and Destructors
483(1)
Strings and C-Strings
484(1)
Size and Capacity
485(2)
Element Access
487(1)
Comparisons
488(1)
Modifiers
489(3)
Substrings and String Concatenation
492(1)
Input/Output Operators
492(1)
Searching and Finding
493(2)
The Value npos
495(2)
Iterator Support for Strings
497(6)
Internationalization
503(3)
Performance
506(1)
Strings and Vectors
506(1)
String Class in Detail
507(22)
Type Definitions and Static Values
507(1)
Create, Copy, and Destroy Operations
508(2)
Operations for Size and Capacity
510(1)
Comparisons
511(1)
Character Access
512(1)
Generating C-Strings and Character Arrays
513(1)
Modifying Operations
514(6)
Searching and Finding
520(4)
Substrings and String Concatenation
524(1)
Input/Output Functions
524(1)
Generating Iterators
525(1)
Allocator Support
526(3)
Numerics
529(54)
Complex Numbers
529(18)
Examples Using Class Complex
530(3)
Operations for Complex Numbers
533(8)
Class complex<> in Detail
541(6)
Valarrays
547(34)
Getting to Know Valarrays
547(6)
Valarray Subsets
553(16)
Class valarray in Detail
569(6)
Valarray Subset Classes in Detail
575(6)
Global Numeric Functions
581(2)
Input/Output Using Stream Classes
583(102)
Common Background of I/O Streams
584(4)
Stream Objects
584(1)
Stream Classes
584(1)
Global Stream Objects
585(1)
Stream Operators
586(1)
Manipulators
586(1)
A Simple Example
587(1)
Fundamental Stream Classes and Objects
588(5)
Classes and Class Hierarchy
588(3)
Global Stream Objects
591(1)
Header Files
592(1)
Standard Stream Operators « and »
593(4)
Output Operator «
593(1)
Input Operator»
594(1)
Input/Output of Special Types
595(2)
State of Streams
597(10)
Constants for the State of Streams
597(1)
Member Functions Accessing the State of Streams
598(2)
Stream State and Boolean Conditions
600(2)
Stream State and Exceptions
602(5)
Standard Input/Output Functions
607(5)
Member Functions for Input
607(3)
Member Functions for Output
610(1)
Example Uses
611(1)
Manipulators
612(3)
How Manipulators Work
612(2)
User-Defined Manipulators
614(1)
Formatting
615(10)
Format Flags
615(2)
Input/Output Format of Boolean Values
617(1)
Field Width, Fill Character, and Adjustment
618(2)
Positive Sign and Uppercase Letters
620(1)
Numeric Base
621(2)
Floating-Point Notation
623(2)
General Formatting Definitions
625(1)
Internationalization
625(2)
File Access
627(10)
File Flags
631(3)
Random Access
634(3)
Using File Descriptors
637(1)
Connecting Input and Output Streams
637(8)
Loose Coupling Using tie ()
637(1)
Tight Coupling Using Stream Buffers
638(3)
Redirecting Standard Streams
641(2)
Streams for Reading and Writing
643(2)
Stream Classes for Strings
645(7)
String Stream Classes
645(4)
char* Stream Classes
649(3)
Input/Output Operators for User-Defined Types
652(11)
Implementing Output Operators
652(2)
Implementing Input Operators
654(2)
Input/Output Using Auxiliary Functions
656(2)
User-Defined Operators Using Unformatted Functions
658(1)
User-Defined Format Flags
659(3)
Conventions for User-Defined Input/Output Operators
662(1)
The Stream Buffer Classes
663(18)
User's View of Stream Buffers
663(2)
Stream Buffer Iterators
665(3)
User-Defined Stream Buffers
668(13)
Performance Issues
681(4)
Synchronization with C's Standard Streams
682(1)
Buffering in Stream Buffers
682(1)
Using Stream Buffers Directly
683(2)
Internationalization
685(42)
Different Character Encodings
686(6)
Wide-Character and Multibyte Text
686(1)
Character Traits
687(4)
Internationalization of Special Characters
691(1)
The Concept of Locales
692(8)
Using Locales
693(5)
Locale Facets
698(2)
Locales in Detail
700(4)
Facts in Detail
704(23)
Numeric Formatting
705(3)
Time and Date Formatting
708(3)
Monetary Formatting
711(4)
Character Classification and Conversion
715(9)
String Collation
724(1)
Internationalized Messages
725(2)
Allocators
727(16)
Using Allocators as an Application Programmer
727(1)
Using Allocators as a Library Programmer
728(4)
The Default Allocator
732(3)
A User-Defined Allocator
735(2)
Allocators in Detail
737(3)
Type Definitions
737(2)
Operations
739(1)
Utilities for Uninitialized Memory in Detail
740(3)
Internet Resources 743(2)
Bibliography 745(2)
Index 747

Supplemental Materials

What is included with this book?

The New copy of this book will include any supplemental materials advertised. Please check the title of the book to determine if it should include any access cards, study guides, lab manuals, CDs, etc.

The Used, Rental and eBook copies of this book are not guaranteed to include any supplemental materials. Typically, only the book itself is included. This is true even if the title states it includes any access cards, study guides, lab manuals, CDs, etc.

Excerpts

In the beginning, I only planned to write a small German book (400 pages or so) about the C++ Standard Library. That was in 1993. Now, in 1999 you see the result--an English book with more than 800 pages of facts, figures, and examples. My goal is to describe the C++ Standard Library so that all (or almost all) your programming questions are answered before you think of the question. Note, however, that this is not a complete description of all aspects of the C++ Standard Library. Instead, I present the most important topics necessary for learning and programming in C++by using its standard library. Each topic is described based on the general concepts;this discussion then leads to the specific details needed to support everyday programming tasks. Specific code examples are provided to help you understand the concepts and the details. That's it--in a nutshell. I hope you get as much pleasure from reading this book as I did from writing it. Enjoy! Acknowledgments This book presents ideas, concepts,solutions, and examples from many sources. In a way it does not seem fair that my name is the only name on the cover. Thus, I'd like to thank all the people and companies who helped and supported me during the past few years. First, I'd like to thank Dietmar Kuhl. Dietmar is an expert on C++, especially on input/output streams and internationalization (he implemented an I/O stream library just for fun). He not only translated major parts of this book from German to English, he also wrote sections of this book using his expertise. In addition, he provided me with invaluable feedback over the years. Second, I'd like to thank all the reviewers and everyone else who gave me their opinion. These people endow the book with a quality it would never have had without their input. (Because the list is extensive, please forgive me for any oversight.) The reviewers for the English version of this book included Chuck Allison, Greg Comeau, James A. Crotinger, Gabriel Dos Reis, Alan Ezust, Nathan Meyers, Werner Mossner, Todd Veldhuizen, Chichiang Wan, Judy Ward, and Thomas Wikehult. The German reviewers included Ralf Boecker, Dirk Herrmann, Dietmar Kuhl, Edda Lorke, Herbert Scheubner, Dominik Strasser, and Martin Weitzel. Additional input was provided by Matt Austern, Valentin Bonnard, Greg Colvin, Beman Dawes, Bill Gibbons, Lois Goldthwaite, Andrew Koenig, Steve Rumbsby, Bjarne Stroustrup, and David Vandevoorde. Special thanks to Dave Abrahams, Janet Cocker, Catherine Ohala, and Maureen Willard who reviewed and edited the whole book very carefully. Their feedback was an incredible contribution to the quality of this book. A special thanks goes to my "personal living dictionary"--Herb Sutter--the author of the famous "Guru of the Week" (a regular series of C++ programming problems that is published on the comp.std.c++.moderated Internet newsgroup). I'd also like to thank all the people and companies who gave me the opportunity to test my examples on different platforms with different compilers. Many thanks to Steve Adamczyk, Mike Anderson, and John Spicer from EDG for their great compiler and their support. It was a big help during the standardization process and the writing of this book. Many thanks to P. J. Plauger and Dinkumware, Ltd, for their early standard-conforming implementation of the C++ Standard Library. Many thanks to Andreas Hommel and Metrowerks for an evaluative version of their CodeWarrior Programming Environment. Many thanks to all the developers of the free GNU and egcs compilers. Many thanks to Microsoft for an evaluative version of Visual C++. Many thanks to Roland Hartinger from Siemens Nixdorf Informations Systems AG for a test version of their C++ compiler. Many thanks to Topjects GmbH for an evaluative version of the ObjectSpace library implementation. And, of course many thanks for those who invented, designed, or implemented C++ or parts of the

Rewards Program