First Edition Winner 1996 Jolt Award for Best Book of the Year.
Learn practical programming and best practices.
Meet the difficult challenges of C++ development.
Build reliable and robust programs.
Design Patterns chapter shows sophisticated use of objects, composition and polymorphism.
Provides a gentle introduction to multithreaded programming, a feature being considered for the next version of Standard C++.
Defensive Programming chapter includes a simple unit-testing framework and debugging techniques.
In-depth treatment of Standard C++ Library facilities including strings, iostreams, and the "STL" algorithms and containers.
Modern usage of templates, including template metaprogramming.
Unravels the perplexities of multiple inheritance.
Shows practical uses for RTTI.
Explores exception handling in depth and clearly explains exception-safe design.
Compliant with the official ISO C++ Standard.
Presents results of current research being considered for inclusion in the next revision of Standard C++.
All code examples freely downloadable, tested on multiple platforms and compilers including the free GNU C++ compiler on Windows/Mac/Linux.
目录
Preface
13
What’s new in the second edition13
What’s in Volume 2 of this book ……14
How to get Volume 2 ……………………14
Prerequisites…………………………. 14
Learning C++……………………….. 14
Goals …………………………………… 16
Chapters ………………………………. 17
Exercises ……………………………… 18
Exercise solutions ………………………..18
Source code………………………….. 18
Language standards……………….. 20
Language support…………………………20
The book’s CD ROM …………….. 20
Seminars, CD Roms & consulting20
Errors ………………………………….. 21
Acknowledgements……………….. 21
Part 1: The Standard C++ Library
23
Library overview…………………… 24
1: Strings
27
What’s in a string ………………….. 27
Creating and initializing C++ strings 29
Operating on strings………………. 31
Appending, inserting and concatenating strings
32
Replacing string characters ……………34
Concatenation using non-member overloaded operators
Searching in strings……………….. 38
Finding in reverse…………………………43
Finding first/last of a set………………..44
Removing characters from strings…..45
Comparing strings ………………………..49
Using iterators……………………………..53
37
Strings and character traits …………….55
A string application……………….. 58
Summary……………………………… 61
Exercises ……………………………… 62
2: Iostreams
63
Why iostreams?…………………….. 63
True wrapping……………………………..65
Iostreams to the rescue…………… 67
Sneak preview of operator overloading68
Inserters and extractors …………………69
Common usage…………………………….70
Line-oriented input……………………….72
File iostreams……………………….. 74
Open modes ………………………………..76
Iostream buffering…………………. 76
Using get( ) with a streambuf…………78
Seeking in iostreams ……………… 78
Creating read/write files ………………..80
stringstreams ………………………… 81
strstreams …………………………….. 81
User-allocated storage…………………..81
Automatic storage allocation………….84
Output stream formatting ……….. 87
Internal formatting data…………………88
An exhaustive example …………………92
Formatting manipulators………… 95
Manipulators with arguments…………96
Creating manipulators……………. 99
Effectors……………………………………100
Iostream examples ………………. 102
Code generation …………………………102
A simple datalogger ……………………110
Counting editor ………………………….117
Breaking up big files …………………..118
Summary……………………………. 120
Exercises ……………………………. 120
3: Templates in depth
121
Nontype template arguments … 121
Default template arguments ….. 122
The typename keyword………… 122
Typedefing a typename ……………….124
Using typename instead of class ….124
Function templates ………………. 124
A string conversion system ………….125
A memory allocation system………..126
Type induction in function templates
129
Taking the address of a generated function template
Chapter 2: Hiding the Implementation
130
7
Local classes in templates …….. 131
Applying a function to an STL sequence
Template-templates ……………… 134
Member function templates ….. 135
131
Why virtual member template functions are disallowed
Nested template classes……………….137
137
Template specializations ………. 137
Full specialization ………………………137
Partial Specialization…………………..137
A practical example ……………………137
Design & efficiency ……………………141
Preventing template bloat…………….141
Explicit instantiation ……………. 143
Explicit specification of template functions
144
Controlling template instantiation144
The inclusion vs. separation models145
The export keyword ……………………145
Template programming idioms 145
The “curiously-recurring template”.145
Traits………………………………………..145
Summary……………………………. 145
4: STL Containers & Iterators147
Containers and iterators ……….. 147
STL reference documentation ………149
The Standard Template Library 149
The basic concepts ………………. 151
Containers of strings ……………. 155
Inheriting from STL containers 157
A plethora of iterators ………….. 159
Iterators in reversible containers …..161
Iterator categories……………………….162
Predefined iterators …………………….163
Basic sequences: vector, list & deque
169
Basic sequence operations……………169
vector ………………………………… 172
Cost of overflowing allocated storage173
Inserting and erasing elements ……..177
deque…………………………………. 179
Converting between sequences …….181
Cost of overflowing allocated storage182
Checked random-access ………………184
list …………………………………….. 185
Special list operations …………………187
Swapping all basic sequences……….191
Robustness of lists………………………192
Performance comparison ……… 193
set……………………………………… 198
Eliminating strtok( ) …………………..199
StreamTokenizer: a more flexible solution
Chapter 2: Hiding the Implementation
201
8
A completely reusable tokenizer …..203
stack ………………………………….. 208
queue…………………………………. 211
Priority queues ……………………. 216
Holding bits………………………… 226
bitset