Contents Preface xvii Acknowledgments xix 1 About this Book 1 1.1 Why this Book I 1.2 What You Should Know Before Reading this Book 2 1.3 Style and Structure of the Book 2 1.4 How to Read this Book 4 1.5 State of the Aft 5 1.6 Example Code and Additiona11nformation 5 1.7 Feedback 5 2 Introduction to C++ and the Standard Library 7 2.1 History 7 2.2 New Language Features 9 2.2.1 Templates 9 2.2.2 Explicit initialization for Fundamental Types 14 2.2.3 Exception Handling 15 2.2.4 Namespaces 16 2.2.5 TVpe hoof 18 2.2.6 KeVword exDlicit 18 2.2.7 New Operators for Type Conversion 19 2.2.8 Initialization of Constant Static Members 20 2.2.9 Definition of maino ZI 2.3 Complexity and the Big-O Notation ZI 3 General Concepts 23 3.1 Namespace std 23 3.2 Header Files 24 3.3 Error and Exception Handling 25 3.3.1 Standard ExceDtion Classes 25 3.3.2 Members of Exception Classes 28 3.3.3 Throwing Standard Exceptions 29 3.3.4 Deriving Standard Exception Classes 30 3.4 Allocators 31 4 Utilities 33 4.1 Pairs 33 4.1.1 Convenience Function make--pair () 36 4.1.2 Examples of Pair Usage 37 4.2 Class auto Dtr 38 4.2.1 Motivation of Class auto Dtr 38 4.2.2 Transfer of Ownership bV auto air 40 4.2.3 auto Dtrs as Members 44 4.2.4 Misusing auto--ptrs 46 4.2.5 auto Dtr ExamDles 47 4.2.6 Class auto Dtr in Detail sl 4.3 Numeric Limits 59 4.4 Auxiliary Functions 66 4.4.1 Processing the Minimum and Maximum 66 4.4.2 Swapping TWo Values 67 4.5 Supplementary Comparison Operators 69 4.6 Header Files <cstddef> and <cstdlib> 71 4.6.1 Definitions in <cstddef> 71 4.6.2 Definitions in <cstdlib> 71 5 The Standard Template Library 73 5.1 STL Components 73 5.2 Containers 75 5.2.1 Sequence Containers 76 5.2.2 Associative Containers sl 5.2.3 Container Adapters 82 5.3 Iterators 83 5.3.1 Examples of Using Associative Containers 86 5.3.2 Iterator Categories 93 5.4 Algorithms 94 5.4.1 Ranges 97 5.4.2 Handling Multiple Ranges 101 5.5 Iterator Adapters 104 5.5.1 Insert lterators 104 5.5.2 Stream lterators 107 5.5.3 Reverse lterators 109 5.6 Manipulating Algorithms 111 5.6.1 "Removing" Elements 111 5.6.2 Manipulating Algorithms and Associative Containers 115 5.6.3 Algorithms versus Member Functions 116 5.7 User-Defined Generic Functions 117 5.8 Functions as Algorithm Arguments 119 5.8. 1 Examples of Using Functions as Algorithm Arguments 119 5.8.2 Predicates 121 5.9 Function Obiects 124 5.9.1 What Are Function Objects? 124 5.9.2 Predefined Function Obiects 131 5.10 Container Elements 134 5.10. 1 Requirements for Container Elements 134 5.10.2 Value Semantics or Reference Semantics 135 5.11 Errors and Exceptions inside the ST1136 5.11.1 Error Handling 137 5.11.2 Exception Handling 139 5.12 ExtendinZ the ST1141 6 STL Containers 143 6.1 Common Container Abilities and Operations 144 6.1.1 Common Container Abilities 144 6.1.2 Common Container Operations 144 6.2 Vectors 148 6.2.1 Abilities of Vectors 148 6.2.2 Vector Operations 150 6.2.3 Using Vectors as Ordinary Arrays 155 6.2.4 Exception Handling 155 6.2.5 Examples of Using Vectors 156 6.2.6 Class vector<bool> 158 6.3 Deques 160 6.3.1 Abilities of Deques 161 6.3.2 Deque Operations 162 6.3.3 Exception Handling 164 6.3.4 Examples of Using Deques 164 6.4 Lists 166 6.4.1 Abilities of Lists 166 6.4.2 List Operations 167 6.4.3 Exception Handling 172 6.4.4 Examples of Using Lists 172 6.5 Sets and Multisets 175 6.5.1 Abilities of Sets and Multisets 176 6.5.2 Set and Multiset ODerations 177 6.5.3 Exception Handling 185 6.5.4 Examples of Using Sets and Multisets 186 6.5.5 Example of SpecifVing the Sorting Criterion at Runtime 191 6.6 Maps and Multimaps 194 6.6.1 Abilities of Maps and Multimaps 195 6.6.2 Map and Multimap Operations 196 6.6.3 Using Maps as Associative Arrays 205 6.6.4 Exception Handling 207 6.6.5 Examples of Using Maps and Multimaps 207 6.6.6 Example with Maps, Strings, and Sorting Criterion at Runtime 213 6.7 Other STL Containers 217 6.7.1 Strings as STL Containers 217 6.7.2 OrdinarV ArraVs as STL Containers 218 6.7.3 Hash Tables 221 6.8 Implementing Reference Semantics 222 6.9 When to Use which Container 226 6.10 Container TVves and Members in Detail 230 6.10.1 TVpe Dennitions 230 6.10.2 Create, Copy, and Destroy Operations 231 6.10.3 NonmodifVing Operations 233 6.10.4 Assignments 236 6.10.5 Direct Element Access 237 6.10.6 Operations to Generate lterators 239 6.10.7 Inserting and Removing Elements 240 6.10.8 Special Member Functions for Lists 244 6.10.9 Allocator Support 246 6.10.10 Overview of Exception Handling in STL Containers 248 7 ST11terators 251 7.1 Header Files for lteratofs 251 7.2 Iterator Categories 251 7.2.1 Input lterators 252 7.2.2 Output lterators 253 7.2.3 Forward lterators 254 7.2.4 Bidirectional lterators 255 7.2.5 Random Access lterators 255 7.2.6 The increment and Decrement Problem of Vector lterators 258 7.3 Auxiliary lterator Functions 259 7.3.1 Stepping lterators Using advance () 259 7.3.2 Processing lterator Distance Using distance () 261 7.3.3 Swapping lterator Values Using iter--swapo 263 7.4 Iterator Adapters 264 7.4.1 Reverse lterators 264 7.4.2 Insert lterators 271 7.4.3 Stream lterators 277 7.5 Iterator Traits 283 7.5.1 Writing Generic Functions for lterators 285 7.5.2 User--Defined lterators 288 8 STL Function Obiects 293 8.1 The Concept of Function Objects 293 8.1.1 Function Objects as Sorting Criteria 294 8.1.2 Function Objects with internal State 296 8.1.3 The Return Value of for--each () 300 8.1.4 Predicates versus Function Objects 302 8.2 Predefined Function Objects 305 8.2.1 Function Adapters 306 8.2.2 Function Adapters for Member Functions 307 8.2.3 Function Adapters for Ordinary Functions 309 8.2.4 User-Defined Function Obiects for Function Adamers 310 8.3 Supplementary ComDosing Function Objects 313 8.3.1 UnarV Compose Function Obiect Adapters 314 8.3.2 Binary Compose Function Object AdaDters 318 9 STL Algorithms 321 9.1 Algorithm Header Files 321 9.2 A12orithm Overview 322 9.2.1 A Brief introduction 322 9.2.2 Classification of Algorithms 323 9.3 AuxiliarV Functions 332 9.4 The for eacho Algorithm 334 9.5 NonmodifVing Algorithms 338 9.5.1 Counting Elements 338 9.5.2 Minimum and Maximum 339 9.5.3 Searching Elements 341 9.5.4 Comparing Ranges 356 9.6 ModifVing Algorithms 363 9.6.1 Copying Elements 363 9.6.2 Transforming and Combining Elements 366 9.6.3 Swapping Elements 370 9.6.4 Assigning New Values 372 9.6.5 Replacing Elements 375 9.7 Removing Algorithms 378 9.7.1 Removing Certain Values 378 9.7.2 Removing Duplicates 381 9.8 Mutating Algorithms 386 9.8.1 Reversing the Order of Elements 386 9.8.2 Rotating Elements 388 9.8.3 Permuting Elements 391 9.8.4 Shuffling Elements 393 9.8.5 Moving Elements to the Front 395 9.9 Sorting Algorithms 397 9.9.1 Sorting All Elements 397 9.9.2 Partial Sorting 400 9.9.3 Sorting According to the nib Element 404 9.9.4 Heap Algorithms 406 9.10 Sorted Range Algorithms 409 9.10.1 Searching Elements 410 9.10.2 Merging Elements 416 9.11 Numeric Algorithms 425 9.11.1 Processing Results 425 9.11.2 Converting Relative and Absolute Values 429 10 Special Containers 435 10.1 Stacks 435 10.1.1 The Core interface 436 10.1.2 Example of Using Stacks 437 10.1.3 Class stack<> in Detail 438 10.1.4 A User-Defined Stack Class 441 10.2 Queues 444 10.2.1 The Core interface 445 10.2.2 Example of Using Queues 446 10.2.3 Class queue<> in Detail 447 10.2.4 A User-Denned Queue Class 450 10.3 Priority Oueues 453 10.3.1 The Core interface 455 10.3.2 Example of Using Priority Queues 455 10.3.3 Class priority--queue<> in Detail 456 10.4 Bitsets 460 10.4.1 Examples of Using Bitsets 460 10.4.2 Class bitset in Detail 463 11 Strings 471 11.1 Motivation 471 11.1.1 A First Example f Extracting a Temporary File Name 472 11.1.2 A Second Example f Extracting Words and Printing Them Backward 476 11.2 Description of the String Classes 479 11.2.1 String Types 479 11.2.2 Operation Overview 481 11.2.3 Constructors and Destructors 483 11.2.4 Strings and C-Strings 484 11.2.5 Size and CaDacitV 485 11.2.6 Element Access 487 11.2.7 Comparisons 488 11.2.8 Modifiers 489 11.2.9 SubstrinZs and String Concatenation 492 11.2. 10 InpuUOutput Operators 492 11.2.11 Searching and Finding 493 11.2. 13 Iterator Support for Strings 497 11.2.14 Intemationalization 503 11.2. 15 Performance 506 11.2.16 Strings and Vectors 506 11.3 String Class in Detail 507 11.3.1 TVpe Definitions and Static Values 507 11.3.2 Create, Copy, and Destroy Operations 508 11.3.3 Operations for Size and CaDacitV 510 11.3.4 Comparisons s11 11.3.5 Character Access 512 11.3.6 Generating C-Strings and Character Arrays 513 11.3.7 ModifVing Operations 514 11.3.8 SearchinZ and Finding 520 11.3.9 Substrings and String Concatenation 524 11.3.10 Input/Output Functions 524 11.3. 11 Generating lterators 525 11.3.12 Allocator Support 526 12 Numerics 529 12.1 Complex Numbers 529 12. 1. 1 ExamDles Using Class Complex 530 12.1.2 Operations for Complex Numbers 533 12.1.3 Class complex<> in Detail 541 12.2 Valarravs 547 12.2.1 Getting to Know Valarrays 547 12.2.2 ValarraV Subsets 553 12.2.3 Class valarrav in Detail 569 12.2.4 Valarrav Subset Classes in Detail 575 12.3 Global Numeric Functions 581 13 Inputloutput Using Stream Classes 583 13.1 Common Background of I/O Streams 584 13.1.1 Stream Objects 584 13.1.2 Stream Classes 584 13.1.3 Global Stream Objects 585 13.1.4 Stream Operators 586 13.1.5 Manipulators 586 13.1.6 A Simple Example 587 13.2 Fundamental Stream Classes and Objects 588 13.2.1 Classes and Class Hierarchy 588 13.2.2 Global Stream Objects 591 13.2.3 Header Files 592 13.3 Standard Stream Operators << and >> 593 13.3.1 Output Operator << 593 13.3.2 Input Operator >> 594 13.3.3 Input/Output of Special Types 595 13.4 State of Streams 597 13.4.1 Constants for the State of Streams 597 13.4.2 Member Functions Accessing the State of Streams 598 13.4.3 Stream State and Boolean Conditions 600 13.4.4 Stream State and Exceptions 602 13.5 Standard input/Output Functions 607 13.5.1 Member Functions for input 607 13.5.2 Member Functions for Output 610 13.5.3 Example Uses 611 13.6 Manipulators 612 13.6.1 How Manipulators Work 612 13.6.2 User-Defined Manipulators 614 13.7 Formatting 615 13.7.1 Format Flags 615 13.7.2 Input/Output Format of Boolean Values 617 13.7.3 Field Width, Fill Character, and Adjustment 618 13.7.4 Positive Sign and Uppercase Letters 620 13.7.5 Numeric Base 621 13.7.6 Floating-Point Notation 623 13.7.7 General Formatting Definitions 625 13.8 Intemationalization 625 13.9 File Access 627 13.9.1 File Flags 631 13.9.2 Random Access 634 13.9.3 Using File Descriptors 637 13.10 Connecting input and OutDut Streams 637 13.10.1 Loose Coupling Using tie () 637 13.10.2 Tight Coupling Using Stream Buffers 638 13.10.3 Redirecting Standard Streams 641 13.10.4 Streams for Reading and Writing 643 13.11 Stream Classes for Strings 645 13.11.1 String Stream Classes 645 13.11.2 char* Stream Classes 649 13.12 InpuUOutput Operators for User-Defined Types 652 13.12.1 Implementing Output Operators 652 13.12.2 Implementing input Operators 654 13.12.3 Input/Output Using Auxiliary Functions 656 13.12.4 User--Denned ODerators Using Unformatted Functions 658 13.12.5 User--Defined Format Flags 659 13.12.6 Conventions for User-Defined input/Output Operators 662 13.13 The Stream Buffer Classes 663 13.13.1 User's View of Stream Buffers 663 13.13.2 Stream Buffer lterators 665 13.13.3 User-Defined Stream Buffers 668 13.14 Performance Issues 681 13.14.1 SVnchronization with C's Standard Streams 682 13.14.2 Buffering in Stream Buffers 682 13.14.3 Using Stream Buffers Directly 683 14 Internationalization 685 14.1 Different Character Encodings 686 14.1.1 Wide-Character and Multibvte Text 686 14.1.2 Character Traits 687 14.1.3 Intemationalization of Special Characters 691 14.2 The Concept of Locales 692 14.2.1 Using Locales 693 14.2.2 Locale Facets 698 14.3 Locales in Detail 700 14.4 Facets in Detail 704 14.4.1 Numeric Formatting 705 14.4.2 Time and Date Formatting 708 14.4.3 MonetarV Formatting 711 14.4.4 Character Classification and Conversion 715 14.4.5 String Collation 724 14.4.6 Internationalized Messages 725 15 Allocators 727 15.1 Using Allocators as an Application Programmer 727 15.2 Using Allocators as a Library Programmer 728 15.3 The Default Allocator 732 15.4 A UseFDefined Allocator 735 15.5 Allocators in Detail 737 15.5.1 TVpe Definitions 737 15.5.2 Operations 739 15.6 Utilities for Uninitialized Memory in Detail 740 Internet Resources 743 Bibliography 745 Index 747