(This page has no text content)
Rheinwerk Computing Explore more of the Rheinwerk Computing library! The Rheinwerk Computing series offers new and established professionals comprehen- sive guidance to enrich their skillsets and enhance their career prospects. Our publica- tions are written by the leading experts in their fields. Each book is detailed and hands-on to help readers develop essential, practical skills that they can apply to their daily work. www.rheinwerk-computing.com Johannes Ernesti, Peter Kaiser Python 3: The Comprehensive Guide 2022, 1036 pages, paperback and e-book www.rheinwerk-computing.com/5566 Jürgen Wolf HTML and CSS: The Comprehensive Guide 2023, 814 pages, paperback and e-book www.rheinwerk-computing.com/5695 Philip Ackermann JavaScript: The Comprehensive Guide 2022, 982 pages, paperback and e-book www.rheinwerk-computing.com/5554 Christian Ullenboom Java: The Comprehensive Guide 2023, 1126 pages, paperback and e-book www.rheinwerk-computing.com/5557 Philip Ackermann Full Stack Web Development: The Comprehensive Guide 2023, 740 pages, paperback and e-book www.rheinwerk-computing.com/5704
Torsten T. Will C++ The Comprehensive Guide
Imprint This e-book is a publication many contributed to, specifically: Editor Megan Fuerst Acquisitions Editor Hareem Shafi German Edition Editor Almut Poll Translation Torsten T. Will Copyeditor Melinda Rankin Cover Design Graham Geary Photo Credit Shutterstock.com: 167285333/© titov Dmitriy; Midjourney.com Layout Design Vera Brauner Production E-Book Graham Geary Typesetting E-Book SatzPro, Germany We hope that you liked this e-book. Please share your feedback with us and read the Service Pages to find out how to contact us. Library of Congress Cataloging-in-Publication Control Number: 2024039385 ISBN 978-1-4932-2626-9 (print) ISBN 978-1-4932-2627-6 (e-book) ISBN 978-1-4932-2628-3 (print and e-book) © 2025 by Rheinwerk Publishing, Inc., Boston (MA) 1st edition 2025 3rd German edition published 2024 by Rheinwerk Verlag, Bonn, Germany
(This page has no text content)
Contents Preface ....................................................................................................................................................... 27 PART I Fundamentals 1 C++: The Comprehensive Guide 33 1.1 New and Modern ................................................................................................................... 33 1.2 “Dan” Chapters ...................................................................................................................... 34 1.3 Presentation in This Book .................................................................................................. 35 1.4 Formatting Used .................................................................................................................... 35 1.5 Let’s Talk Lingo ....................................................................................................................... 36 2 Programming in C++ 37 2.1 Compiling .................................................................................................................................. 38 2.2 Translation Phases ................................................................................................................ 39 2.3 Current Compilers ................................................................................................................. 39 2.3.1 Gnu C++ ..................................................................................................................... 40 2.3.2 Clang++ from LLVM ............................................................................................... 40 2.3.3 Microsoft Visual C++ ............................................................................................. 40 2.3.4 Compiler in the Container ................................................................................... 40 2.4 Development Environments ............................................................................................ 41 2.5 The Command Line under Ubuntu ................................................................................. 43 2.5.1 Create a Program .................................................................................................... 44 2.5.2 Automate with Makefile ...................................................................................... 45 2.6 The Visual Studio Code IDE under Windows ............................................................. 46 2.7 Speed Up the Sample Program ........................................................................................ 53 3 C++ for Newcomers 557
Contents4 The Basic Building Blocks of C++ 63 4.1 A Quick Overview .................................................................................................................. 66 4.1.1 Comments ................................................................................................................ 66 4.1.2 The “include” Directive ......................................................................................... 66 4.1.3 The Standard Library ............................................................................................. 66 4.1.4 The “main()” Function ........................................................................................... 67 4.1.5 Types ........................................................................................................................... 67 4.1.6 Variables .................................................................................................................... 67 4.1.7 Initialization ............................................................................................................. 68 4.1.8 Output on the Console ......................................................................................... 69 4.1.9 Statements ............................................................................................................... 69 4.2 A Detailed Walkthrough .................................................................................................... 70 4.2.1 Spaces, Identifiers, and Tokens .......................................................................... 71 4.2.2 Comments ................................................................................................................ 73 4.2.3 Functions and Arguments ................................................................................... 74 4.2.4 Side Effect Operators ............................................................................................ 75 4.2.5 The “main” Function ............................................................................................. 76 4.2.6 Statements ............................................................................................................... 78 4.2.7 Expressions ............................................................................................................... 80 4.2.8 Allocations ................................................................................................................ 81 4.2.9 Types ........................................................................................................................... 83 4.2.10 Variables: Declaration, Definition, and Initialization ................................. 89 4.2.11 Initialize with “auto” ............................................................................................. 90 4.2.12 Details on the Include Directive ........................................................................ 92 4.2.13 Modules ..................................................................................................................... 93 4.2.14 Input and Output ................................................................................................... 94 4.2.15 The “std” Namespace ............................................................................................ 95 4.3 Operators .................................................................................................................................. 97 4.3.1 Operators and Operands ..................................................................................... 97 4.3.2 Overview of Operators .......................................................................................... 98 4.3.3 Arithmetic Operators ............................................................................................ 99 4.3.4 Bit-by-Bit Arithmetic ............................................................................................. 100 4.3.5 Composite Assignment ........................................................................................ 103 4.3.6 Post- and Preincrement and Post- and Predecrement ............................... 104 4.3.7 Relational Operators ............................................................................................. 104 4.3.8 Logical Operators .................................................................................................... 105 4.3.9 Pointer and Dereference Operators ................................................................. 106 4.3.10 Special Operators ................................................................................................... 107 4.3.11 Function-Like Operators ....................................................................................... 109 4.3.12 Operator Sequence ................................................................................................ 1108
Contents4.4 Built-In Data Types ............................................................................................................... 111 4.4.1 Overview .................................................................................................................... 112 4.4.2 Initialize Built-In Data Types ............................................................................... 114 4.4.3 Integers ...................................................................................................................... 114 4.4.4 Floating-Point Numbers ....................................................................................... 127 4.4.5 Truth Values ............................................................................................................. 141 4.4.6 Character Types ...................................................................................................... 143 4.4.7 Complex Numbers ................................................................................................. 146 4.5 Undefined Behavior ............................................................................................................. 149 5 Good Code, 1st Dan: Writing Readable Code 151 5.1 Comments ................................................................................................................................. 151 5.2 Documentation ...................................................................................................................... 152 5.3 Indentations and Line Length .......................................................................................... 153 5.4 Lines per Function and File ................................................................................................ 154 5.5 Brackets and Spaces ............................................................................................................. 154 5.6 Names ......................................................................................................................................... 156 6 Higher Data Types 159 6.1 The String Type “string” ..................................................................................................... 160 6.1.1 Initialization ............................................................................................................. 161 6.1.2 Functions and Methods ....................................................................................... 162 6.1.3 Other String Types ................................................................................................. 163 6.1.4 For Viewing Only: “string_view” ....................................................................... 164 6.2 Streams ...................................................................................................................................... 166 6.2.1 Input and Output Operators ............................................................................... 166 6.2.2 “getline” ..................................................................................................................... 168 6.2.3 Files for Input and Output ................................................................................... 168 6.2.4 Manipulators ........................................................................................................... 170 6.2.5 The “endl” Manipulator ........................................................................................ 172 6.3 Container and Pointer ......................................................................................................... 172 6.3.1 Container ................................................................................................................... 172 6.3.2 Parameterized Types ............................................................................................. 1739
Contents6.4 The Simple Sequence Containers ................................................................................... 174 6.4.1 “array” ........................................................................................................................ 174 6.4.2 “vector” ...................................................................................................................... 176 6.5 Algorithms ................................................................................................................................ 179 6.6 Pointers and C-Arrays .......................................................................................................... 180 6.6.1 Pointer Types ........................................................................................................... 180 6.6.2 C-Arrays ..................................................................................................................... 180 7 Functions 181 7.1 Declaration and Definition of a Function ................................................................... 182 7.2 Function Type .......................................................................................................................... 183 7.3 Using Functions ...................................................................................................................... 184 7.4 Defining a Function .............................................................................................................. 185 7.5 More about Parameters ...................................................................................................... 186 7.5.1 Call-by-Value ............................................................................................................ 186 7.5.2 Call-by-Reference ................................................................................................... 187 7.5.3 Constant References ............................................................................................. 188 7.5.4 Call as Value, Reference, or Constant Reference? ....................................... 189 7.6 Functional Body ...................................................................................................................... 190 7.7 Converting Parameters ....................................................................................................... 192 7.8 Overloading Functions ........................................................................................................ 194 7.9 Default Parameter ................................................................................................................ 196 7.10 Arbitrary Number of Arguments .................................................................................... 198 7.11 Alternative Notation for Function Declaration ........................................................ 198 7.12 Specialties ................................................................................................................................. 199 7.12.1 “noexcept” ................................................................................................................ 199 7.12.2 Inline Functions ....................................................................................................... 200 7.12.3 “constexpr” ............................................................................................................... 200 7.12.4 Deleted Functions .................................................................................................. 201 7.12.5 Specialties in Class Methods .............................................................................. 20110
Contents8 Statements in Detail 203 8.1 The Statement Block ............................................................................................................ 206 8.1.1 Standalone Blocks and Variable Scope ........................................................... 207 8.2 The Empty Statement .......................................................................................................... 208 8.3 Declaration Statement ........................................................................................................ 209 8.3.1 Structured Binding ................................................................................................. 210 8.4 The Expression Statement ................................................................................................. 211 8.5 The “if” Statement ................................................................................................................ 212 8.5.1 “if” with Initializer .................................................................................................. 215 8.5.2 Compile-Time “if” ................................................................................................... 215 8.6 The “while” Loop ................................................................................................................... 216 8.7 The “do-while” Loop ............................................................................................................ 218 8.8 The “for” Loop ......................................................................................................................... 219 8.9 The Range-Based “for” Loop ............................................................................................ 221 8.10 The “switch” Statement ..................................................................................................... 222 8.11 The “break” Statement ....................................................................................................... 227 8.12 The “continue” Statement ................................................................................................ 228 8.13 The “return” Statement ..................................................................................................... 228 8.14 The “goto” Statement ......................................................................................................... 229 8.15 The “try-catch” Block and “throw” ............................................................................... 231 8.16 Summary ................................................................................................................................... 232 9 Expressions in Detail 233 9.1 Calculations and Side Effects ........................................................................................... 234 9.2 Types of Expressions ............................................................................................................ 235 9.3 Literals ........................................................................................................................................ 236 9.4 Identifiers .................................................................................................................................. 237 9.5 Parentheses .............................................................................................................................. 237 9.6 Function Call and Index Access ....................................................................................... 238 9.7 Assignment .............................................................................................................................. 238 9.8 Type Casting ............................................................................................................................ 24011
Contents10 Error Handling 243 10.1 Error Handling with Error Codes ..................................................................................... 245 10.2 What Is an Exception? ......................................................................................................... 248 10.2.1 Throwing and Handling Exceptions ................................................................. 249 10.2.2 Unwinding the Call Stack .................................................................................... 250 10.3 Minor Error Handling ........................................................................................................... 251 10.4 Throwing the Exception Again: “rethrow” ................................................................ 251 10.5 The Order in “catch” ............................................................................................................. 252 10.5.1 No “finally” ............................................................................................................... 253 10.5.2 Standard Library Exceptions ............................................................................... 253 10.6 Types for Exceptions ............................................................................................................ 254 10.7 When an Exception Falls Out of “main” ..................................................................... 255 11 Good Code, 2nd Dan: Modularization 257 11.1 Program, Library, Object File ............................................................................................ 257 11.2 Modules ..................................................................................................................................... 258 11.3 Separating Functionalities ................................................................................................ 259 11.4 A Modular Example Project .............................................................................................. 260 11.4.1 Namespaces ............................................................................................................. 263 11.4.2 Implementation ...................................................................................................... 264 11.4.3 Using the Library ..................................................................................................... 270 PART II Object-Oriented Programming and More 12 From Structure to Class 275 12.1 Initialization ............................................................................................................................. 278 12.2 Returning Custom Types .................................................................................................... 279 12.3 Methods Instead of Functions ......................................................................................... 280 12.4 The Better “print” ................................................................................................................. 283 12.5 An Output Like Any Other ................................................................................................. 28512
Contents12.6 Defining Methods Inline ..................................................................................................... 286 12.7 Separate Implementation and Definition .................................................................. 287 12.8 Initialization via Constructor ........................................................................................... 288 12.8.1 Member Default Values in Declaration .......................................................... 291 12.8.2 Constructor Delegation ........................................................................................ 292 12.8.3 Default Values for Constructor Parameters .................................................. 293 12.8.4 Do Not Call the “init” Method in the Constructor ....................................... 294 12.8.5 Exceptions in the Constructor ............................................................................ 295 12.9 Struct or Class? ........................................................................................................................ 295 12.9.1 Encapsulation .......................................................................................................... 297 12.9.2 “public” and “private”, Struct and Class ......................................................... 297 12.9.3 Data with “struct”, Behavior with “class” ...................................................... 298 12.9.4 Initialization of Types with Private Data ........................................................ 298 12.10 Interim Recap .......................................................................................................................... 299 12.11 Using Custom Data Types .................................................................................................. 300 12.11.1 Using Classes as Values ........................................................................................ 302 12.11.2 Using Constructors ................................................................................................ 305 12.11.3 Type Conversions .................................................................................................... 306 12.11.4 Encapsulate and Decapsulate ............................................................................ 308 12.11.5 Give Types a Local Name ...................................................................................... 312 12.12 Type Inference with “auto” .............................................................................................. 315 12.13 Custom Classes in Standard Containers ...................................................................... 319 12.13.1 Three-Way Comparison: The Spaceship Operator ...................................... 321 13 Namespaces and Qualifiers 323 13.1 The “std” Namespace .......................................................................................................... 324 13.2 Anonymous Namespace ..................................................................................................... 327 13.3 “static” Makes Local ............................................................................................................. 329 13.4 “static” Likes to Share ......................................................................................................... 330 13.5 Remote Initialization or “static inline” Data Fields ............................................... 332 13.6 Guaranteed to Be Initialized at Compile Time with “constinit” ...................... 333 13.7 “static” Makes Permanent ................................................................................................ 333 13.8 “inline namespace” .............................................................................................................. 335 13.9 Interim Recap .......................................................................................................................... 33613
Contents13.10 “const” ....................................................................................................................................... 337 13.10.1 Const Parameters ................................................................................................ 338 13.10.2 Const Methods ..................................................................................................... 339 13.10.3 “const” Variables ................................................................................................. 341 13.10.4 Const Returns ....................................................................................................... 342 13.10.5 “const” Together with “static” ........................................................................ 346 13.10.6 Even More Constant with “constexpr” ......................................................... 347 13.10.7 “if constexpr” for Compile-Time Decisions ................................................. 350 13.10.8 C++20: “consteval” .............................................................................................. 352 13.10.9 “if consteval” ......................................................................................................... 354 13.10.10 Un-“const” with “mutable” .............................................................................. 355 13.10.11 Const-Correctness ............................................................................................... 355 13.10.12 Summary ................................................................................................................ 357 13.11 Volatile with “volatile” ....................................................................................................... 357 14 Good Code, 3rd Dan: Testing 361 14.1 Types of Tests .......................................................................................................................... 361 14.1.1 Refactoring ............................................................................................................ 362 14.1.2 Unit Tests ............................................................................................................... 363 14.1.3 Social or Solitary ................................................................................................... 364 14.1.4 Doppelgangers ..................................................................................................... 366 14.1.5 Suites ....................................................................................................................... 367 14.2 Frameworks ............................................................................................................................. 368 14.2.1 Arrange, Act, Assert ............................................................................................. 370 14.2.2 Frameworks to Choose From ........................................................................... 371 14.3 Boost.Test ................................................................................................................................. 372 14.4 Helper Macros for Assertions ........................................................................................... 376 14.5 An Example Project with Unit Tests .............................................................................. 379 14.5.1 Private and Public Testing ................................................................................ 380 14.5.2 An Automatic Test Module .............................................................................. 381 14.5.3 Compile Test .......................................................................................................... 384 14.5.4 Assemble the Test Suite Yourself ................................................................... 384 14.5.5 Testing Private Members .................................................................................. 388 14.5.6 Parameterized Tests ........................................................................................... 38914
Contents15 Inheritance 391 15.1 Relationships ........................................................................................................................... 392 15.1.1 Has-a Composition ................................................................................................ 392 15.1.2 Has-a Aggregation ................................................................................................. 392 15.1.3 Is-a Inheritance ........................................................................................................ 393 15.1.4 Instance-of versus Is-a Relationship ................................................................ 394 15.2 Inheritance in C++ ................................................................................................................. 394 15.3 Has-a versus Is-a .................................................................................................................... 395 15.4 Finding Commonalities ....................................................................................................... 396 15.5 Derived Types Extend .......................................................................................................... 398 15.6 Overriding Methods ............................................................................................................. 399 15.7 How Methods Work ............................................................................................................. 400 15.8 Virtual Methods ..................................................................................................................... 402 15.9 Constructors in Class Hierarchies ................................................................................... 404 15.10 Type Conversion in Class Hierarchies ........................................................................... 405 15.10.1 Converting Up the Inheritance Hierarchy ...................................................... 405 15.10.2 Downcasting the Inheritance Hierarchy ........................................................ 406 15.10.3 References Also Retain Type Information ...................................................... 406 15.11 When to Use Virtual? ........................................................................................................... 407 15.12 Other Designs for Extensibility ....................................................................................... 409 16 The Lifecycle of Classes 411 16.1 Creation and Destruction ................................................................................................... 412 16.2 Temporary: Short-Lived Values ....................................................................................... 414 16.3 The Destructor to the Constructor ................................................................................. 416 16.3.1 No Destructor Needed .......................................................................................... 418 16.3.2 Resources in the Destructor ................................................................................ 418 16.4 Yoda Condition ....................................................................................................................... 420 16.5 Construction, Destruction, and Exceptions ............................................................... 421 16.6 Copy ............................................................................................................................................. 423 16.7 Assignment Operator .......................................................................................................... 426 16.8 Removing Methods .............................................................................................................. 42915
Contents16.9 Move Operations ................................................................................................................... 430 16.9.1 What the Compiler Generates ........................................................................... 434 16.10 Operators .................................................................................................................................. 435 16.11 Custom Operators in a Data Type .................................................................................. 438 16.12 Special Class Forms ............................................................................................................... 446 16.12.1 Abstract Classes and Methods ........................................................................... 446 16.12.2 Enumeration Classes ............................................................................................. 448 17 Good Code, 4th Dan: Security, Quality, and Sustainability 451 17.1 The Rule of Zero ..................................................................................................................... 451 17.1.1 The Big Five ............................................................................................................... 451 17.1.2 Helper Construct by Prohibition ........................................................................ 452 17.1.3 The Rule of Zero and Its Application ................................................................ 453 17.1.4 Exceptions to the Rule of Zero ........................................................................... 454 17.1.5 Rule of Zero, Rule of Three, Rule of Five, Rule of Four and a Half ........... 456 17.2 Resource Acquisition Is Initialization ........................................................................... 457 17.2.1 An Example with C ................................................................................................. 457 17.2.2 Owning Raw Pointers ............................................................................................ 459 17.2.3 From C to C++ .......................................................................................................... 460 17.2.4 It Doesn't Always Have to Be an Exception ................................................... 462 17.2.5 Multiple Constructors ........................................................................................... 463 17.2.6 Multiphase Initialization ..................................................................................... 464 17.2.7 Define Where It Is Needed .................................................................................. 464 17.2.8 “new” without Exceptions .................................................................................. 464 18 Specials for Classes 467 18.1 Allowed to See Everything: “friend” Classes ............................................................ 467 18.1.1 “friend class” Example .......................................................................................... 469 18.2 Nonpublic Inheritance ......................................................................................................... 471 18.2.1 Impact on the Outside World ............................................................................. 473 18.2.2 Nonpublic Inheritance in Practice ..................................................................... 475 18.3 Signature Classes as Interfaces ....................................................................................... 477 18.4 Multiple Inheritance ............................................................................................................ 48116
Contents18.4.1 Multiple Inheritance in Practice ........................................................................ 483 18.4.2 Caution with Pointer Type Conversions ......................................................... 487 18.4.3 The Observer Pattern as a Practical Example ............................................... 489 18.5 Diamond-Shaped Multiple Inheritance: “virtual” for Class Hierarchies ...... 490 18.6 Literal Data Types: “constexpr” for Constructors ................................................... 495 19 Good Code, 5th Dan: Classical Object-Oriented Design 497 19.1 Objects in C++ ......................................................................................................................... 499 19.2 Object-Oriented Design ...................................................................................................... 500 19.2.1 SOLID .......................................................................................................................... 500 19.2.2 Don't Be STUPID ...................................................................................................... 516 PART III Advanced Topics 20 Pointers 521 20.1 Addresses .................................................................................................................................. 522 20.2 Pointer ........................................................................................................................................ 523 20.3 Dangers of Aliasing ............................................................................................................... 525 20.4 Heap Memory and Stack Memory ................................................................................. 526 20.4.1 The Stack ................................................................................................................... 526 20.4.2 The Heap ................................................................................................................... 528 20.5 Smart Pointers ........................................................................................................................ 530 20.5.1 “unique_ptr” ............................................................................................................ 532 20.5.2 “shared_ptr” ............................................................................................................. 536 20.6 Raw Pointers ............................................................................................................................ 539 20.7 C-Arrays ...................................................................................................................................... 543 20.7.1 Calculating with Pointers .................................................................................... 544 20.7.2 Decay of C-Arrays ................................................................................................... 545 20.7.3 Dynamic C-Arrays ................................................................................................... 547 20.7.4 String Literals ........................................................................................................... 548 20.8 Iterators ..................................................................................................................................... 55017
Contents20.9 Pointers as Iterators ............................................................................................................. 551 20.10 Pointers in Containers ......................................................................................................... 552 20.11 The Exception: When Cleanup Is Not Necessary ..................................................... 552 21 Macros 555 21.1 The Preprocessor .................................................................................................................... 556 21.2 Beware of Missing Parenthesis ....................................................................................... 560 21.3 Feature Macros ....................................................................................................................... 561 21.4 Information about the Source Code ............................................................................. 561 21.5 Warning about Multiple Executions ............................................................................. 562 21.6 Type Variability of Macros ................................................................................................. 563 21.7 Summary ................................................................................................................................... 566 22 Interface to C 567 22.1 Working with Libraries ........................................................................................................ 568 22.2 C Header .................................................................................................................................... 569 22.3 C Resources ............................................................................................................................... 572 22.4 “void” Pointers ....................................................................................................................... 572 22.5 Reading Data ........................................................................................................................... 573 22.6 The Main Program ................................................................................................................. 575 22.7 Summary ................................................................................................................................... 575 23 Templates 577 23.1 Function Templates .............................................................................................................. 578 23.1.1 Overloading .............................................................................................................. 579 23.1.2 A Type as Parameter .............................................................................................. 580 23.1.3 Function Body of a Function Template ........................................................... 580 23.1.4 Values as Template Parameters ........................................................................ 583 23.1.5 Many Functions ....................................................................................................... 58418
Contents23.1.6 Parameters with Extras ........................................................................................ 585 23.1.7 Method Templates are Just Function Templates ........................................ 587 23.2 Function Templates in the Standard Library ............................................................. 588 23.2.1 Ranges instead of Containers as Template Parameters ........................... 589 23.2.2 Example: Information about Numbers ........................................................... 592 23.3 A Class as a Function ............................................................................................................ 593 23.3.1 Values for a “Function” Parameter ................................................................... 594 23.3.2 C Function Pointer .................................................................................................. 595 23.3.3 The Somewhat Different Function ................................................................... 597 23.3.4 Practical Functors ................................................................................................... 600 23.3.5 Algorithms with Functors .................................................................................... 602 23.3.6 Anonymous Functions: a.k.a. Lambda Expressions .................................... 602 23.3.7 Template Functions without “template”, but with “auto” ...................... 608 23.4 C++ Concepts ........................................................................................................................... 609 23.4.1 How to Read Concepts .......................................................................................... 609 23.4.2 How to Use Concepts ............................................................................................ 612 23.4.3 How to Write Concepts ........................................................................................ 614 23.4.4 Semantic Constraints ............................................................................................ 615 23.4.5 Interim Recap ........................................................................................................... 616 23.5 Template Classes ................................................................................................................... 616 23.5.1 Implementing Class Templates ......................................................................... 617 23.5.2 Implementing Methods of Class Templates ................................................. 618 23.5.3 Creating Objects from Class Templates ......................................................... 620 23.5.4 Class Templates with Multiple Formal Data Types .................................... 623 23.5.5 Class Templates with Nontype Parameters .................................................. 625 23.5.6 Class Templates with Defaults .......................................................................... 626 23.5.7 Specializing Class Templates .............................................................................. 628 23.6 Templates with Variable Argument Count ................................................................ 630 23.6.1 “sizeof ...” Operator ................................................................................................ 633 23.6.2 Convert Parameter Pack to Tuple ..................................................................... 633 23.7 Custom Literals ....................................................................................................................... 634 23.7.1 What Are Literals? .................................................................................................. 635 23.7.2 Naming Rules ........................................................................................................... 635 23.7.3 Literal Processing Phases ..................................................................................... 636 23.7.4 Overloading Variants ............................................................................................ 636 23.7.5 User-Defined Literal Using Template .............................................................. 638 23.7.6 Raw or Cooked ......................................................................................................... 641 23.7.7 Automatically Merged .......................................................................................... 642 23.7.8 Unicode Literals ....................................................................................................... 64219
ContentsPART IV The Standard Library 24 Containers 647 24.1 Basics ........................................................................................................................................... 648 24.1.1 Recurring ................................................................................................................... 648 24.1.2 Abstract ..................................................................................................................... 649 24.1.3 Operations ................................................................................................................ 650 24.1.4 Complexity ................................................................................................................ 651 24.1.5 Containers and Their Iterators ........................................................................... 653 24.1.6 Ranges Simplify Iterators ..................................................................................... 656 24.1.7 Ranges, Views, Concepts, Adapters, Generators, and Algorithms ......... 659 24.1.8 Algorithms ................................................................................................................ 660 24.2 Iterator Basics .......................................................................................................................... 660 24.2.1 Iterators from Containers .................................................................................... 662 24.2.2 More Functionality with Iterators .................................................................... 663 24.3 Allocators: Memory Issues ................................................................................................ 665 24.4 Container Commonalities .................................................................................................. 668 24.5 An Overview of the Standard Container Classes ..................................................... 669 24.5.1 Type Aliases of Containers .................................................................................. 670 24.6 The Sequential Container Classes .................................................................................. 673 24.6.1 Commonalities and Differences ........................................................................ 675 24.6.2 Methods of Sequence Containers ..................................................................... 677 24.6.3 “vector” ...................................................................................................................... 679 24.6.4 “array” ........................................................................................................................ 698 24.6.5 “deque” ...................................................................................................................... 704 24.6.6 “list” ............................................................................................................................ 708 24.6.7 “forward_list” .......................................................................................................... 711 24.7 Associative and Ordered .................................................................................................... 716 24.7.1 Commonalities and Differences ........................................................................ 717 24.7.2 Methods of Ordered Associative Containers ................................................ 718 24.7.3 “set” ............................................................................................................................ 719 24.7.4 “map” ......................................................................................................................... 733 24.7.5 “multiset” .................................................................................................................. 740 24.7.6 “multimap” ............................................................................................................... 745 24.8 Only Associative and Not Guaranteed ......................................................................... 749 24.8.1 Hash Tables .............................................................................................................. 749 24.8.2 Commonalities and Differences ........................................................................ 754 24.8.3 Methods of Unordered Associative Containers ........................................... 75620
Contents24.8.4 “unordered_set” ..................................................................................................... 757 24.8.5 “unordered_map” .................................................................................................. 766 24.8.6 “unordered_multiset” .......................................................................................... 770 24.8.7 “unordered_multimap” ....................................................................................... 776 24.9 Container Adapters ............................................................................................................... 779 24.10 Special Cases: “string”, “basic_string”, and “vector<char>” ............................ 781 24.11 Special Cases: “vector<bool>”, “array<bool,n>”, and “bitset<n>” ................ 782 24.11.1 Dynamic and Compact: “vector<bool>” ......................................................... 783 24.11.2 Static: “array<bool,n>” and “bitset<n>” ......................................................... 783 24.12 Special Case: Value Array with “valarray<>” ............................................................ 786 24.12.1 Element Properties ................................................................................................. 787 24.12.2 Initialize ..................................................................................................................... 789 24.12.3 Assignment .............................................................................................................. 789 24.12.4 Insert and Delete .................................................................................................... 790 24.12.5 Accessing ................................................................................................................... 790 24.12.6 Specialty: Manipulate All Data .......................................................................... 790 24.12.7 Specialty: Slicing and Masking ........................................................................... 791 25 Container Support 795 25.1 Algorithms ................................................................................................................................ 796 25.2 Iterators and Ranges ............................................................................................................ 798 25.3 Iterator Adapter ..................................................................................................................... 800 25.4 Algorithms of the Standard Library ............................................................................... 800 25.5 Parallel Execution .................................................................................................................. 802 25.6 Lists of Algorithm Functions and Range Adapters ................................................. 805 25.6.1 Range Adapters and Views .................................................................................. 806 25.6.2 Ranges as Parameters (and More) .................................................................... 813 25.6.3 List of Nonmodifying Algorithms ...................................................................... 815 25.6.4 Inherently Modifying Algorithms ..................................................................... 820 25.6.5 Algorithms for Partitions ..................................................................................... 825 25.6.6 Algorithms for Sorting and Fast Searching in Sorted Ranges ................. 826 25.6.7 Set Algorithms Represented by a Sorted Range ........................................... 827 25.6.8 Heap Algorithms ..................................................................................................... 829 25.6.9 Minimum and Maximum .................................................................................... 830 25.6.10 Various Algorithms ................................................................................................ 83021
Comments 0
Loading comments...
Reply to Comment
Edit Comment