Preface |
|
xix | |
|
|
1 | (30) |
|
Writing a Simple C++ Program |
|
|
2 | (3) |
|
Compiling and Executing Our Program |
|
|
3 | (2) |
|
A First Look at Input/Output |
|
|
5 | (5) |
|
Standard Input and Output Objects |
|
|
6 | (1) |
|
A Program that Uses the IO Library |
|
|
6 | (4) |
|
|
10 | (1) |
|
|
11 | (9) |
|
|
12 | (2) |
|
|
14 | (3) |
|
|
17 | (1) |
|
Reading an Unknown Number of Inputs |
|
|
18 | (2) |
|
|
20 | (5) |
|
|
21 | (3) |
|
A First Look at Member Functions |
|
|
24 | (1) |
|
|
25 | (6) |
|
|
28 | (1) |
|
|
28 | (3) |
|
|
31 | (272) |
|
Variables and Basic Types |
|
|
33 | (44) |
|
|
34 | (3) |
|
|
34 | (3) |
|
|
37 | (1) |
|
|
37 | (6) |
|
|
43 | (13) |
|
|
45 | (1) |
|
|
46 | (2) |
|
|
48 | (2) |
|
Variable Initialization Rules |
|
|
50 | (2) |
|
Declarations and Definitions |
|
|
52 | (2) |
|
|
54 | (1) |
|
Define Variables Where They Are Used |
|
|
55 | (1) |
|
|
56 | (2) |
|
|
58 | (3) |
|
|
61 | (1) |
|
|
62 | (1) |
|
|
63 | (4) |
|
Writing Our Own Header Files |
|
|
67 | (10) |
|
Designing Our Own Headers |
|
|
67 | (2) |
|
A Brief Introduction to the Preprocessor |
|
|
69 | (4) |
|
|
73 | (1) |
|
|
73 | (4) |
|
|
77 | (32) |
|
Namespace using Declarations |
|
|
78 | (2) |
|
|
80 | (10) |
|
Defining and Initializing strings |
|
|
80 | (1) |
|
Reading and Writing strings |
|
|
81 | (2) |
|
|
83 | (5) |
|
Dealing with the Characters of a string |
|
|
88 | (2) |
|
|
90 | (5) |
|
Defining and Initializing vectors |
|
|
91 | (2) |
|
|
93 | (2) |
|
|
95 | (6) |
|
|
100 | (1) |
|
|
101 | (8) |
|
Defining and Initializing bitsets |
|
|
102 | (2) |
|
|
104 | (3) |
|
|
107 | (1) |
|
|
107 | (2) |
|
|
109 | (38) |
|
|
110 | (4) |
|
Defining and Initializing Arrays |
|
|
110 | (3) |
|
|
113 | (1) |
|
|
114 | (16) |
|
|
115 | (1) |
|
Defining and Initializing Pointers |
|
|
116 | (3) |
|
|
119 | (3) |
|
Using Pointers to Access Array Elements |
|
|
122 | (4) |
|
Pointers and the const Qualifier |
|
|
126 | (4) |
|
C-Style Character Strings |
|
|
130 | (11) |
|
Dynamically Allocating Arrays |
|
|
134 | (5) |
|
Interfacing to Older Code |
|
|
139 | (2) |
|
|
141 | (6) |
|
Pointers and Multidimensioned Arrays |
|
|
143 | (2) |
|
|
145 | (1) |
|
|
145 | (2) |
|
|
147 | (44) |
|
|
149 | (3) |
|
Relational and Logical Operators |
|
|
152 | (2) |
|
|
154 | (5) |
|
Using bitset Objects or Integral Values |
|
|
156 | (2) |
|
Using the Shift Operators for IO |
|
|
158 | (1) |
|
|
159 | (3) |
|
Assignment Is Right Associative |
|
|
160 | (1) |
|
Assignment Has Low Precedence |
|
|
160 | (1) |
|
Compound Assignment Operators |
|
|
161 | (1) |
|
Increment and Decrement Operators |
|
|
162 | (2) |
|
|
164 | (1) |
|
|
165 | (2) |
|
|
167 | (1) |
|
|
168 | (1) |
|
Evaluating Compound Expressions |
|
|
168 | (6) |
|
|
168 | (2) |
|
|
170 | (2) |
|
|
172 | (2) |
|
The new and delete Expressions |
|
|
174 | (4) |
|
|
178 | (13) |
|
When Implicit Type Conversions Occur |
|
|
179 | (1) |
|
The Arithmetic Conversions |
|
|
180 | (1) |
|
Other Implicit Conversions |
|
|
181 | (2) |
|
|
183 | (1) |
|
When Casts Might Be Useful |
|
|
184 | (1) |
|
|
184 | (2) |
|
|
186 | (2) |
|
|
188 | (1) |
|
|
188 | (3) |
|
|
191 | (34) |
|
|
192 | (1) |
|
|
193 | (1) |
|
Compound Statements (Blocks) |
|
|
193 | (1) |
|
|
194 | (1) |
|
|
195 | (4) |
|
The if Statement else Branch |
|
|
197 | (2) |
|
|
199 | (5) |
|
|
200 | (1) |
|
Control Flow within a switch |
|
|
201 | (2) |
|
|
203 | (1) |
|
switch Expression and Case Labels |
|
|
203 | (1) |
|
Variable Definitions inside a switch |
|
|
204 | (1) |
|
|
204 | (3) |
|
|
207 | (3) |
|
Omitting Parts of the for Header |
|
|
209 | (1) |
|
Multiple Definitions in the for Header |
|
|
210 | (1) |
|
|
210 | (2) |
|
|
212 | (2) |
|
|
214 | (1) |
|
|
214 | (1) |
|
try Blocks and Exception Handling |
|
|
215 | (5) |
|
|
216 | (1) |
|
|
217 | (2) |
|
|
219 | (1) |
|
Using the Preprocessor for Debugging |
|
|
220 | (5) |
|
|
223 | (1) |
|
|
223 | (2) |
|
|
225 | (58) |
|
|
226 | (3) |
|
|
227 | (1) |
|
|
228 | (1) |
|
|
229 | (16) |
|
|
230 | (2) |
|
|
232 | (5) |
|
vector and Other Container Parameters |
|
|
237 | (1) |
|
|
238 | (3) |
|
Managing Arrays Passed to Functions |
|
|
241 | (2) |
|
main: Handling Command-Line Options |
|
|
243 | (1) |
|
Functions with Varying Parameters |
|
|
244 | (1) |
|
|
245 | (6) |
|
Functions with No Return Value |
|
|
245 | (1) |
|
Functions that Return a Value |
|
|
246 | (3) |
|
|
249 | (2) |
|
|
251 | (3) |
|
|
253 | (1) |
|
|
254 | (2) |
|
|
255 | (1) |
|
|
255 | (1) |
|
|
256 | (2) |
|
|
258 | (7) |
|
Defining the Body of a Member Function |
|
|
259 | (2) |
|
Defining a Member Function Outside the Class |
|
|
261 | (1) |
|
Writing the Sales_item Constructor |
|
|
262 | (2) |
|
Organizing Class Code Files |
|
|
264 | (1) |
|
|
265 | (11) |
|
|
268 | (1) |
|
Function Matching and Argument Conversions |
|
|
269 | (1) |
|
The Three Steps in Overload Resolution |
|
|
270 | (2) |
|
Argument-Type Conversions |
|
|
272 | (4) |
|
|
276 | (7) |
|
|
280 | (1) |
|
|
280 | (3) |
|
|
283 | (20) |
|
An Object-Oriented Library |
|
|
284 | (3) |
|
|
287 | (3) |
|
Managing the Output Buffer |
|
|
290 | (3) |
|
|
293 | (6) |
|
Using File Stream Objects |
|
|
293 | (3) |
|
|
296 | (3) |
|
A Program to Open and Check Input Files |
|
|
299 | (1) |
|
|
299 | (4) |
|
|
302 | (1) |
|
|
302 | (1) |
|
Part II Containers and Algorithms |
|
|
303 | (124) |
|
|
305 | (50) |
|
Defining a Sequential Container |
|
|
307 | (4) |
|
Initializing Container Elements |
|
|
307 | (2) |
|
Constraints on Types that a Container Can Hold |
|
|
309 | (2) |
|
Iterators and Iterator Ranges |
|
|
311 | (5) |
|
|
314 | (1) |
|
Some Container Operations Invalidate Iterators |
|
|
315 | (1) |
|
Sequence Container Operations |
|
|
316 | (14) |
|
|
316 | (1) |
|
|
317 | (1) |
|
Adding Elements to a Sequential Container |
|
|
318 | (3) |
|
|
321 | (2) |
|
Container Size Operations |
|
|
323 | (1) |
|
|
324 | (2) |
|
|
326 | (2) |
|
|
328 | (2) |
|
|
330 | (3) |
|
capacity and reserve Members |
|
|
331 | (2) |
|
Deciding Which Container to Use |
|
|
333 | (2) |
|
|
335 | (13) |
|
Other Ways to Construct strings |
|
|
338 | (1) |
|
Other Ways to Change a string |
|
|
339 | (2) |
|
|
341 | (2) |
|
|
343 | (3) |
|
|
346 | (2) |
|
|
348 | (7) |
|
|
350 | (1) |
|
|
351 | (2) |
|
|
353 | (1) |
|
|
353 | (2) |
|
|
355 | (36) |
|
Preliminaries: the pair Type |
|
|
356 | (2) |
|
|
358 | (2) |
|
|
360 | (12) |
|
|
360 | (1) |
|
|
361 | (1) |
|
|
362 | (1) |
|
|
362 | (2) |
|
|
364 | (3) |
|
Finding and Retrieving a map Element |
|
|
367 | (1) |
|
Erasing Elements from a map |
|
|
368 | (1) |
|
|
369 | (1) |
|
A Word Transformation Map |
|
|
369 | (3) |
|
|
372 | (3) |
|
|
373 | (1) |
|
Building a Word-Exclusion Set |
|
|
374 | (1) |
|
The multimap and multiset Types |
|
|
375 | (4) |
|
Adding and Removing Elements |
|
|
376 | (1) |
|
Finding Elements in a multimap or multiset |
|
|
376 | (3) |
|
Using Containers: Text-Query Program |
|
|
379 | (12) |
|
Design of the Query Program |
|
|
380 | (2) |
|
|
382 | (1) |
|
Using the TextQuery Class |
|
|
383 | (2) |
|
Writing the Member Functions |
|
|
385 | (3) |
|
|
388 | (1) |
|
|
388 | (3) |
|
|
391 | (36) |
|
|
392 | (3) |
|
A First Look at the Algorithms |
|
|
395 | (10) |
|
|
396 | (2) |
|
Algorithms that Write Container Elements |
|
|
398 | (2) |
|
Algorithms that Reorder Container Elements |
|
|
400 | (5) |
|
|
405 | (14) |
|
|
406 | (1) |
|
|
407 | (5) |
|
|
412 | (3) |
|
|
415 | (1) |
|
The Five Iterator Categories |
|
|
416 | (3) |
|
Structure of Generic Algorithms |
|
|
419 | (2) |
|
Algorithm Parameter Patterns |
|
|
419 | (1) |
|
Algorithm Naming Conventions |
|
|
420 | (1) |
|
Container-Specific Algorithms |
|
|
421 | (6) |
|
|
424 | (1) |
|
|
424 | (3) |
|
Part III Classes and Data Abstraction |
|
|
427 | (128) |
|
|
429 | (46) |
|
Class Definitions and Declarations |
|
|
430 | (10) |
|
Class Definitions: A Recap |
|
|
430 | (2) |
|
Data Abstraction and Encapsulation |
|
|
432 | (2) |
|
More on Class Definitions |
|
|
434 | (3) |
|
Class Declarations versus Definitions |
|
|
437 | (2) |
|
|
439 | (1) |
|
The Implicit this Pointer |
|
|
440 | (4) |
|
|
444 | (7) |
|
Name Lookup in Class Scope |
|
|
447 | (4) |
|
|
451 | (14) |
|
The Constructor Initializer |
|
|
453 | (5) |
|
Default Arguments and Constructors |
|
|
458 | (1) |
|
|
458 | (3) |
|
Implicit Class-Type Conversions |
|
|
461 | (3) |
|
Explicit Initialization of Class Members |
|
|
464 | (1) |
|
|
465 | (2) |
|
|
467 | (8) |
|
|
469 | (1) |
|
|
469 | (4) |
|
|
473 | (1) |
|
|
473 | (2) |
|
|
475 | (30) |
|
|
476 | (6) |
|
The Synthesized Copy Constructor |
|
|
479 | (1) |
|
Defining Our Own Copy Constructor |
|
|
480 | (1) |
|
|
481 | (1) |
|
|
482 | (2) |
|
|
484 | (2) |
|
A Message-Handling Example |
|
|
486 | (6) |
|
|
492 | (13) |
|
Defining Smart Pointer Classes |
|
|
495 | (4) |
|
Defining Valuelike Classes |
|
|
499 | (3) |
|
|
502 | (1) |
|
|
502 | (3) |
|
Overloaded Operations and Conversions |
|
|
505 | (50) |
|
Defining an Overloaded Operator |
|
|
506 | (7) |
|
Overloaded Operator Design |
|
|
510 | (3) |
|
Input and Output Operators |
|
|
513 | (4) |
|
Overloading the Output Operator<< |
|
|
513 | (2) |
|
Overloading the Input Operator>> |
|
|
515 | (2) |
|
Arithmetic and Relational Operators |
|
|
517 | (3) |
|
|
518 | (2) |
|
|
520 | (1) |
|
|
520 | (2) |
|
|
522 | (1) |
|
|
523 | (3) |
|
Increment and Decrement Operators |
|
|
526 | (4) |
|
Call Operator and Function Objects |
|
|
530 | (5) |
|
Using Function Objects with Library Algorithms |
|
|
531 | (2) |
|
Library-Defined Function Objects |
|
|
533 | (2) |
|
Function Adaptors for Function Objects |
|
|
535 | (1) |
|
Conversions and Class Types |
|
|
535 | (20) |
|
Why Conversions Are Useful |
|
|
536 | (1) |
|
|
537 | (4) |
|
Argument Matching and Conversions |
|
|
541 | (3) |
|
Overload Resolution and Class Arguments |
|
|
544 | (3) |
|
Overloading, Conversions, and Operators |
|
|
547 | (5) |
|
|
552 | (1) |
|
|
552 | (3) |
|
Part IV Object-Oriented and Generic Programming |
|
|
555 | (130) |
|
Object-Oriented Programming |
|
|
557 | (66) |
|
|
558 | (2) |
|
Defining Base and Derived Classes |
|
|
560 | (17) |
|
|
560 | (2) |
|
|
562 | (1) |
|
|
563 | (3) |
|
virtual and Other Member Functions |
|
|
566 | (4) |
|
Public, Private, and Protected Inheritance |
|
|
570 | (5) |
|
Friendship and Inheritance |
|
|
575 | (1) |
|
Inheritance and Static Members |
|
|
576 | (1) |
|
Conversions and Inheritance |
|
|
577 | (3) |
|
Derived-to-Base Conversions |
|
|
577 | (3) |
|
Conversions from Base to Derived |
|
|
580 | (1) |
|
Constructors and Copy Control |
|
|
580 | (10) |
|
Base-Class Constructors and Copy Control |
|
|
580 | (1) |
|
Derived-Class Constructors |
|
|
581 | (3) |
|
Copy Control and Inheritance |
|
|
584 | (3) |
|
|
587 | (2) |
|
Virtuals in Constructors and Destructors |
|
|
589 | (1) |
|
Class Scope under Inheritance |
|
|
590 | (5) |
|
Name Lookup Happens at Compile Time |
|
|
590 | (1) |
|
Name Collisions and Inheritance |
|
|
591 | (1) |
|
Scope and Member Functions |
|
|
592 | (2) |
|
Virtual Functions and Scope |
|
|
594 | (1) |
|
|
595 | (2) |
|
Containers and Inheritance |
|
|
597 | (1) |
|
Handle Classes and Inheritance |
|
|
598 | (9) |
|
|
599 | (3) |
|
|
602 | (1) |
|
|
603 | (4) |
|
|
607 | (16) |
|
An Object-Oriented Solution |
|
|
609 | (1) |
|
|
610 | (2) |
|
|
612 | (1) |
|
|
613 | (3) |
|
|
616 | (2) |
|
|
618 | (3) |
|
|
621 | (1) |
|
|
621 | (2) |
|
Templates and Generic Programming |
|
|
623 | (62) |
|
|
624 | (12) |
|
Defining a Function Template |
|
|
625 | (2) |
|
Defining a Class Template |
|
|
627 | (1) |
|
|
628 | (2) |
|
|
630 | (2) |
|
Nontype Template Parameters |
|
|
632 | (1) |
|
|
633 | (3) |
|
|
636 | (7) |
|
Template Argument Deduction |
|
|
637 | (5) |
|
Function-Template Explicit Arguments |
|
|
642 | (1) |
|
Template Compilation Models |
|
|
643 | (4) |
|
|
647 | (19) |
|
Class-Template Member Functions |
|
|
651 | (4) |
|
Template Arguments for Nontype Parameters |
|
|
655 | (1) |
|
Friend Declarations in Class Templates |
|
|
655 | (3) |
|
Queue and QueueItem Friend Declarations |
|
|
658 | (2) |
|
|
660 | (4) |
|
|
664 | (1) |
|
static Members of Class Templates |
|
|
665 | (1) |
|
|
666 | (5) |
|
Defining the Handle Class |
|
|
667 | (1) |
|
|
668 | (3) |
|
|
671 | (8) |
|
Specializing a Function Template |
|
|
672 | (3) |
|
Specializing a Class Template |
|
|
675 | (2) |
|
Specializing Members but Not the Class |
|
|
677 | (1) |
|
Class-Template Partial Specializations |
|
|
678 | (1) |
|
Overloading and Function Templates |
|
|
679 | (6) |
|
|
683 | (1) |
|
|
683 | (2) |
|
|
685 | (124) |
|
|
687 | (66) |
|
|
688 | (24) |
|
Throwing an Exception of Class Type |
|
|
689 | (2) |
|
|
691 | (2) |
|
|
693 | (2) |
|
|
695 | (1) |
|
|
696 | (1) |
|
Function Try Blocks and Constructors |
|
|
696 | (1) |
|
Exception Class Hierarchies |
|
|
697 | (3) |
|
Automatic Resource Deallocation |
|
|
700 | (2) |
|
|
702 | (4) |
|
|
706 | (5) |
|
Function Pointer Exception Specifications |
|
|
711 | (1) |
|
|
712 | (19) |
|
|
712 | (5) |
|
|
717 | (1) |
|
|
718 | (2) |
|
|
720 | (4) |
|
Classes, Namespaces, and Scope |
|
|
724 | (3) |
|
Overloading and Namespaces |
|
|
727 | (3) |
|
|
730 | (1) |
|
Multiple and Virtual Inheritance |
|
|
731 | (22) |
|
|
731 | (3) |
|
Conversions and Multiple Base Classes |
|
|
734 | (3) |
|
Copy Control for Multiply Derived Classes |
|
|
737 | (1) |
|
Class Scope under Multiple Inheritance |
|
|
737 | (3) |
|
|
740 | (2) |
|
Virtual Base Class Declaration |
|
|
742 | (2) |
|
Special Initialization Semantics |
|
|
744 | (4) |
|
|
748 | (1) |
|
|
748 | (5) |
|
Specialized Tools and Techniques |
|
|
753 | (56) |
|
Optimizing Memory Allocation |
|
|
754 | (18) |
|
|
754 | (1) |
|
|
755 | (4) |
|
operator new and operator delete Functions |
|
|
759 | (2) |
|
Placement new Expressions |
|
|
761 | (1) |
|
Explicit Destructor Invocation |
|
|
762 | (1) |
|
Class Specific new and delete |
|
|
763 | (3) |
|
A Memory-Allocator Base Class |
|
|
766 | (6) |
|
Run-Time Type Identification |
|
|
772 | (8) |
|
The dynamic_cast Operator |
|
|
773 | (2) |
|
|
775 | (2) |
|
|
777 | (2) |
|
|
779 | (1) |
|
|
780 | (6) |
|
Declaring a Pointer to Member |
|
|
781 | (2) |
|
Using a Pointer to Class Member |
|
|
783 | (3) |
|
|
786 | (6) |
|
A Nested-Class Implementation |
|
|
787 | (4) |
|
Name Lookup in Nested Class Scope |
|
|
791 | (1) |
|
Union: A Space-Saving Class |
|
|
792 | (4) |
|
|
796 | (1) |
|
Inherently Nonportable Features |
|
|
797 | (12) |
|
|
798 | (1) |
|
|
799 | (2) |
|
Linkage Directives: extern ``C'' |
|
|
801 | (4) |
|
|
805 | (1) |
|
|
805 | (4) |
|
|
809 | (34) |
|
A.1 Library Names and Headers |
|
|
810 | (1) |
|
A.2 A Brief Tour of the Algorithms |
|
|
811 | (14) |
|
A.2.1 Algorithms to Find an Object |
|
|
811 | (2) |
|
A.2.2 Other Read-Only Algorithms |
|
|
813 | (1) |
|
A.2.3 Binary-Search Algorithms |
|
|
814 | (1) |
|
A.2.4 Algorithms that Write Container Elements |
|
|
815 | (2) |
|
A.2.5 Partitioning and Sorting Algorithms |
|
|
817 | (1) |
|
A.2.6 General Reordering Operations |
|
|
818 | (2) |
|
A.2.7 Permutation Algorithms |
|
|
820 | (1) |
|
A.2.8 Set Algorithms for Sorted Sequences |
|
|
821 | (1) |
|
A.2.9 Minimum and Maximum Values |
|
|
822 | (1) |
|
A.2.10 Numeric Algorithms |
|
|
823 | (2) |
|
A.3 The IO Library Revisited |
|
|
825 | (18) |
|
|
825 | (1) |
|
A.3.2 Many Manipulators Change the Format State |
|
|
825 | (1) |
|
A.3.3 Controlling Output Formats |
|
|
826 | (7) |
|
A.3.4 Controlling Input Formatting |
|
|
833 | (1) |
|
A.3.5 Unformatted Input/Output Operations |
|
|
834 | (1) |
|
A.3.6 Single-Byte Operations |
|
|
834 | (1) |
|
A.3.7 Multi-Byte Operations |
|
|
835 | (2) |
|
A.3.8 Random Access to a Stream |
|
|
837 | (3) |
|
A.3.9 Reading and Writing to the Same File |
|
|
840 | (3) |
Index |
|
843 | |