Chapter 1 Getting Started(新增评注29条) 1
1.1 Writing a Simple C++ Program 2
1.1.1 Compiling and Executing Our Program 3
1.2 A First Look at Input/Output 5
1.2.1 Standard Input and Output Objects 5
1.2.2 A Program that Uses the IO Library 5
1.3 A Word About Comments 8
1.4 Control Structures 10
1.4.1 The while Statement 10
1.4.2 The for Statement 12
1.4.3 The if Statement 14
1.4.4 Reading an Unknown Number of Inputs 15
1.5 Introducing Classes 17
1.5.1 The Sales_item Class 17
1.5.2 A First Look at Member Functions 20
1.6 The C++ Program 21
Part Ⅰ The Basics 23
Chapter 2 Variables and Basic Types(新增评注42条) 25
2.1 Primitive Built-in Types 26
2.1.1 Integral Types 26
2.1.2 Floating-Point Types 28
2.2 Literal Constants 29
2.3 Variables 33
2.3.1 What Is a Variable? 34
2.3.2 The Name of a Variable 36
2.3.3 Defining Objects 37
2.3.4 Variable Initialization Rules 39
2.3.5 Declarations and Definitions 41
2.3.6 Scope of a Name 42
2.3.7 Define Variables Where They Are Used 43
2.4 const Qualifier 44
2.5 References 46
2.6 Typedef Names 48
2.7 Enumerations 48
2.8 Class Types 49
2.9 Writing Our Own Header Files 52
2.9.1 Designing Our Own Headers 53
2.9.2 A Brief Introduction to the Preprocessor 55
Chapter 3 Library Types(新增评注30条) 59
3.1 Namespace using Declarations 60
3.2 Library string Type 62
3.2.1 Defining and Initializing strings 62
3.2.2 Reading and Writing strings 62
3.2.3 Operations on strings 64
3.2.4 Dealing with the Characters of a string 68
3.3 Library vector Type 70
3.3.1 Defining and Initializing vectors 70
3.3.2 Operations on vectors 72
3.4 Introducing Iterators 74
3.4.1 Iterator Arithmetic 78
3.5 Library bitset Type 79
3.5.1 Defining and Initializing bitsets 79
3.5.2 Operations on bitsets 81
Chapter 4 Arrays and Pointers(新增评注33条) 85
4.1 Arrays 86
4.1.1 Defining and Initializing Arrays 86
4.1.2 Operations on Arrays 89
4.2 Introducing Pointers 89
4.2.1 What Is a Pointer? 90
4.2.2 Defining and Initializing Pointers 91
4.2.3 Operations on Pointers 94
4.2.4 Using Pointers to Access Array Elements 96
4.2.5 Pointers and the const Qualifier 99
4.3 C-Style Character Strings 102
4.3.1 Dynamically Allocating Arrays 106
4.3.2 Interfacing to Older Code 110
4.4 Multidimensioned Arrays 111
4.4.1 Pointers and Multidimensioned Arrays 113
Chapter 5 Expressions(新增评注49条) 115
5.1 Arithmetic Operators 117
5.2 Relational and Logical Operators 119
5.3 The Bitwise Operators 121
5.3.1 Using bitset Objects or Integral Values 123
5.3.2 Using the Shift Operators for IO 124
5.4 Assignment Operators 125
5.4.1 Assignment Is Right Associative 125
5.4.2 Assignment Has Low Precedence 126
5.4.3 Compound Assignment Operators 127
5.5 Increment and Decrement Operators 127
5.6 The Arrow Operator 129
5.7 The Conditional Operator 130
5.8 The sizeof Operator 131
5.9 Comma Operator 132
5.10 Evaluating Compound Expressions 132
5.10.1 Precedence 132
5.10.2 Associativity 133
5.10.3 Order of Evaluation 135
5.11 The new and delete Expressions 137
5.12 Type Conversions 140
5.12.1 When Implicit Type Conversions Occur 141
5.12.2 The Arithmetic Conversions 142
5.12.3 Other Implicit Conversions 143
5.12.4 Explicit Conversions 145
5.12.5 When Casts Might Be Useful 145
5.12.6 Named Casts 145
5.12.7 Old-Style Casts 147
Chapter 6 Statements(新增评注29条) 149
6.1 Simple Statements 150
6.2 Declaration Statements 151
6.3 Compound Statements(Blocks) 151
6.4 Statement Scope 152
6.5 The if Statement 153
6.5.1 The if Statement else Branch 154
6.6 The switch Statement 156
6.6.1 Using a switch 156
6.6.2 Control Flow within a switch 157
6.6.3 The default Label 158
6.6.4 switch Expression and Case Labels 159
6.6.5 Variable Definitions inside a switch 159
6.7 The while Statement 160
6.8 The for Loop Statement 162
6.8.1 Omitting Parts of the for Header 163
6.8.2 Multiple Definitions in the forHeader 164
6.9 The do while Statement 165
6.10 The break Statement 166
6.11 The continue Statement 167
6.12 The goto Statement 168
6 13 try Blocks and Exception Handling 169
6.13.1 A throw Expression 169
6.13.2 The try Block 170
6.13.3 Standard Exceptions 172
6.14 Using the Preprocessorfor Debugging 173
Chapter 7 Functions(新增评注56条) 175
7.1 Defining a Function 176
7.1.1 Function Return Type 177
7.1.2 Function Parameter List 178
7.2 Argument Passing 179
7.2.1 Nonreference Parameters 179
7.2.2 Reference Parameters 181
7.2.3 vector and Other Container Parameters 185
7.2.4 Array Parameters 186
7.2.5 Managing Arrays Passed to Functions 188
7.2.6 main:Handling Command-Line Options 190
7.2.7 Functions with Varying Parameters 191
7.3 The return Statement 191
7.3.1 Functions with No Return Value 191
7.3.2 Functions that Return a Value 192
7.3.3 Recursion 195
7.4 Function Declarations 196
7.4.1 Default Arguments 197
7.5 Local Objects 199
7.5.1 Automatic Objects 199
7.5.2 Static Local Objects 200
7.6 Inline Functions 200
7.7 Class Member Functions 202
7.7.1 Defining the Body of a Member Function 203
7.7.2 Defining a Member Function Outside the Class 204
7.7.3 Writing the Sales item Constructor 205
7.7.4 Organizing Class Code Files 207
7.8 Overloaded Functions 208
7.8.1 Overloading and Scope 210
7.8.2 Function Matching and Argument Conversions 211
7.8.3 The Three Steps in Overload Resolution 212
7.8.4 Argument-Type Conversions 214
7.9 Pointers to Functions 217
Chapter 8 The IO Library(新增评注11条) 221
8.1 An Object-Oriented Library 222
8.2 Condition States 224
8.3 Managing the Output Buffer 227
8 4 File Input and Output 229
8.4.1 Using File Stream Objects 229
8.4.2 File Modes 232
8.4.3 A Program to Open and Check Input Files 234
8.5 String Streams 234
Part Ⅱ Containers and Algorithms 237
Chapter 9 Sequential Containers(新增评注54条) 239
9.1 Defining a Sequential Container 240
9.1.1 Initializing Container Elements 241
9.1.2 Constraints on Types that a Container Can Hold 243
9.2 Iterators and Iterator Ranges 244
9.2.1 Iterator Ranges 246
9.2.2 Some Container Operations Invalidate Iterators 247
9.3 Sequence Container Operations 248
9.3.1 Container Typedefs 248
9.3.2 begin and end Members 249
9.3.3 Adding Elements to a Sequential Container 249
9.3.4 Relational Operators 252
9.3.5 Container Size Operations 254
9.3.6 Accessing Elements 255
9.3.7 Erasing Elements 256
9.3.8 Assignment and swap 258
9.4 How a vector Grows 259
9.4.1 capacity and reserve Members 260
9.5 Deciding Which Container to Use 262
9.6 strings Revisited 264
9.6.1 Other Ways to Construct strings 266
9.6.2 Other Ways to Change a string 267
9.6.3 string-Only Operations 268
9.6.4 string Search Operations 270
9.6.5 Comparing strings 272
9.7 Container Adaptors 274
9.7.1 Stack Adaptor 275
9.7.2 Queue and Priority Queue 276
Chapter 10 Associative Containers(新增评注22条) 279
10.1 Preliminaries:the pair Type 280
10.2 Associative Containers 282
10.3 The map Type 283
10.3.1 Defining a map 283
10.3.2 Types Defined by map 284
10.3.3 Adding Elements to a map 285
10.3.4 Subscripting a map 285
10.3.5 Using map::insert 287
10.3.6 Finding and Retrieving a map Element 289
10.3.7 Erasing Elements from a map 290
10.3.8 Iterating across a map 290
10.3.9 A Word Transformation Map 291
10.4 The set Type 293
10.4.1 Defining and Using sets 293
10.4.2 Building a Word-Exclusion Set 295
10.5 The multimap and multiset Types 296
10.5.1 Adding and Removing Elements 296
10.5.2 Finding Elements in a multimap or multiset 296
10.6 Using Containers:Text-Query Program 299
10.6.1 Design of the Query Program 300
10.6.2 TextQuery Class 301
10.6.3 Using the TextQuery Class 302
10.6.4 Writing the Member Functions 304
Chapter 11 Generic Algorithms(新增评注18条) 307
11.1 Overview 308
11.2 A First Look at the Algorithms 311
11.2.1 Read-Only Algorithms 311
11.2.2 Algorithms that Write Container Elements 313
11.2.3 Algorithms that Reorder Container Elements 315
11.3 Revisiting Iterators 319
11.3.1 Insert Iterators 319
11.3.2 iostream Iterators 320
11.3.3 Reverse Iterators 324
11.3.4 const Iterators 327
11.3.5 The Five Iterator Categories 327
11.4 Structure of Generic Algorithms 330
11.4.1 Algorithm Parameter Patterns 330
11.4.2 Algorithm Naming Conventions 331
11.5 Container-Specific Algorithms 332
Part Ⅲ Classes and Data Abstraction 335
Chapter 12 Classes(新增评注26条) 337
12.1 Class Definitions and Declarations 338
12.1.1 Class Definitions:A Recap 338
12.1.2 Data Abstraction and Encapsulation 339
12.1.3 More on Class Definitions 341
12.1.4 Class Declarations versus Definitions 344
12.1.5 Class Objects 345
12.2 The Implicit this Pointer 346
12.3 Class Scope 349
12.3.1 Name Lookup in Class Scope 351
12.4 Constructors 355
12.4.1 The Constructor Initializer 357
12.4.2 Default Arguments and Constructors 360
12.4.3 The Default Constructor 361
12.4.4 Implicit Class-Type Conversions 363
12.4.5 Explicit Initialization of Class Members 365
12.5 Friends 366
12.6 static Class Members 368
12.6.1 static Member Functions 369
12.6.2 static Data Members 370
Chapter 13 Copy Control(新增评注30条) 373
13.1 The Copy Constructor 374
13.1.1 The Synthesized Copy Constructor 377
13.1.2 Defining Our Own Copy Constructor 377
13.1.3 Preventing Copies 378
13.2 The Assignment Operator 379
13.3 The Destructor 380
13.4 A Message-Handling Example 382
13.5 Managing Pointer Members 387
13.5.1 Defining Smart Pointer Classes 389
13.5.2 Defining Valuelike Classes 393
Chapter 14 Overloaded Operations and Conversions(新增评注31条) 395
14.1 Defining an Overloaded Operator 396
14.1.1 Overloaded Operator Design 399
14.2 Input and Output Operators 402
14.2.1 Overloading the Output Operator<< 402
14.2.2 Overloading the Input Operator>> 404
14.3 Arithmetic and Relational Operators 405
14.3.1 Equality Operators 406
14.3.2 Relational Operators 407
14.4 Assignment Operators 408
14.5 Subscript Operator 409
14.6 Member Access Operators 410
14.7 Increment and Decrement Operators 413
14.8 Call Operator and Function Objects 416
14.8.1 Using Function Objects with Library Algorithms 417
14.8.2 Library-Defined Function Objects 418
14.8.3 Function Adaptors for Function Objects 419
14.9 Conversions and Class Types 420
14.9.1 Why Conversions Are Useful 421
14.9.2 Conversion Operators 421
14.9.3 Argument Matching and Conversions 424
14.9.4 Overload Resolution and Class Arguments 427
14.9.5 Overloading,Conversions,and Operators 430
Part Ⅳ Object-Oriented and Generic Programming 435
Chapter 15 Object-Oriented Programming(新增评注56条) 437
15.1 OOP:An Overview 438
15.2 Defining Base and Derived Classes 439
15.2.1 Defining a Base Class 440
15.2.2 protected Members 441
15.2.3 Derived Classes 442
15.2.4 virtual and Other Member Functions 445
15.2.5 Public,Private,and Protected Inheritance 448
15.2.6 Friendship and Inheritance 452
15.2.7 Inheritance and Static Members 452
15.3 Conversions and Inheritance 453
15.3.1 Derived-to-Base Conversions 453
15.3.2 Conversions from Base to Derived 455
15.4 Constructors and Copy Control 456
15.4.1 Base-Class Constructors and Copy Control 456
15.4.2 Derived-Class Constructors 456
15.4.3 Copy Control and Inheritance 459
15.4.4 Virtual Destructors 462
15.4.5 Virtuals in Constructors and Destructors 463
15.5 Class Scope under Inheritance 464
15.5.1 Name Lookup Happens at CompileTime 464
15.5.2 Name Collisions and Inheritance 465
15.5.3 Scope and Member Functions 466
15.5.4 Virtual Functions and Scope 467
15.6 Pure Virtual Functions 468
15.7 Containers and Inheritance 469
15.8 Handle Classes and Inheritance 470
15.8.1 A Pointerlike Handle 471
15.8.2 Cloning an Unknown Type 473
15.8.3 Using the Handle 475
15.9 Text Queries Revisited 478
15.9.1 An Object-Oriented Solution 479
15.9.2 A Valuelike Handle 480
15.9.3 The Query_base Class 482
15.9.4 The Query Handle Class 483
15.9.5 The Derived Classes 485
15.9.6 The eval Functions 487
Chapter 16 Templates and Generic Programming(新增评注31条) 491
16.1 Template Definitions 492
16.1.1 Defining a Function Template 492
16.1.2 Defining a Class Template 494
16.1.3 Template Parameters 495
16.1.4 Template Type Parameters 497
16.1.5 Nontype Template Parameters 499
16.1.6 Writing Generic Programs 500
16.2 Instantiation 501
16.2.1 Template Argument Deduction 503
16.2.2 Function-Template Explicit Arguments 506
16.3 Template Compilation Models 508
16.4 Class Template Members 511
16.4.1 Class-Template Member Functions 513
16.4.2 Template Arguments for Nontype Parameters 517
16.4.3 Friend Declarations in Class Templates 517
16.4.4 Queue and Queue Item Friend Declarations 520
16.4.5 Member Templates 522
16.4.6 The Complete Queue Class 524
16.4.7 static Members of Class Templates 525
16.5 A Generic Handle Class 526
16.5.1 Defining the Handle Class 527
16.5.2 Using the Handle 528
16.6 Template Specializations 530
16.6.1 Specializing a Function Template 531
16.6.2 Specializing a Class Template 533
16.6.3 Specializing Members but Not the Class 535
16.6.4 Class-Template Partial Specializations 536
16.7 Overloading and Function Templates 537
Part Ⅴ Advanced Topics 541
Chapter 17 Tools for Large Programs(新增评注37条) 543
17.1 Exception Handling 544
17.1.1 Throwing an Exception of Class Type 545
17.1.2 Stack Unwinding 546
17.1.3 Catching an Exception 548
17.1.4 Rethrow 549
17.1.5 The Catch-All Handler 550
17.1.6 Function Try Blocks and Constructors 550
17.1.7 Exception Class Hierarchies 551
17.1.8 Automatic Resource Deallocation 553
17.1.9 The auto_ptr Class 555
17.1.10 Exception Specifications 559
17.1.11 Function Pointer Exception Specifications 562
17.2 Namespaces 563
17.2.1 Namespace Definitions 563
17.2.2 Nested Namespaces 567
17.2.3 Unnamed Namespaces 568
17.2.4 Using Namespace Members 569
17.2.5 Classes,Namespaces,and Scope 573
17.2.6 Overloading and Namespaces 575
17.2.7 Namespaces and Templates 578
17.3 Multiple and Virtual Inheritance 578
17.3.1 Multiple Inheritance 578
17.3.2 Conversions and Multiple Base Classes 581
17.3.3 Copy Control for Multiply Derived Classes 583
17.3.4 Class Scope under Multiple Inheritance 583
17.3.5 Virtual Inheritance 586
17.3.6 Virtual Base Class Declaration 587
17.3.7 Special Initialization Semantics 589
Chapter 18 Specialized Tools and Techniques(新增评注22条) 593
18.1 Optimizing Memory Allocation 594
18.1.1 Memory Allocation in C++ 594
18.1.2 The allocator Class 595
18.1.3 operator new and operator delete Functions 598
18.1.4 Placement new Expressions 600
18.1.5 Explicit Destructor Invocation 601
18.1.6 Class Specific new and delete 601
18.1.7 A Memory-Allocator Base Class 603
18.2 Run-Time Type Identification 608
18.2.1 The dynamic_cast Operator 609
18.2.2 The typeid Operator 611
18.2.3 Using RTTI 612
18.2.4 The type_info Class 614
18.3 Pointer to Class Member 615
18.3.1 Declaring a Pointer to Member 615
18.3.2 Using a Pointer to Class Member 617
18.4 Nested Classes 620
18.4.1 A Nested-Class Implementation 620
18.4.2 Name Lookup in Nested Class Scope 623
18.5 Union:A Space-Saving Class 625
18.6 Local Classes 627
18.7 Inherently Nonportable Features 629
18.7.1 Bit-fields 629
18.7.2 volatile Qualifier 630
18.7.3 Linkage Directives:extern"C" 632
Appendix A The Library 635
A.1 Library Names and Headers 636
A.2 A Brief Tour of the Algorithms 637
A.2.1 Algorithms to Find an Object 637
A.2.2 Other Read-Only Algorithms 638
A.2.3 Binary-Search Algorithms 639
A.2.4 Algorithms that Write Container Elements 639
A.2.5 Partitioning and Sorting Algorithms 641
A.2.6 General Reordering Operations 643
A.2.7 Permutation Algorithms 644
A.2.8 Set Algorithms for Sorted Sequences 645
A.2.9 Minimum and Maximum Values 646
A.2.10 Numeric Algorithms 646
A.3 The IO Library Revisited 648
A.3.1 Format State 648
A.3.2 Many Manipulators Change the Format State 648
A.3.3 Controlling Output Formats 649
A.3.4 Controlling Input Formatting 654
A.3.5 Unformatted Input/Output Operations 655
A.3.6 Single-Byte Operations 655
A.3.7 Multi-Byte Operations 656
A.3.8 Random Access to a Stream 658
A.3.9 Reading and Writing to the Same File 660