Preface xxiii
Chapter 1 Getting Started
1.1 Writing a Simple C++Program
1.1.1 Compiling and Executing Our Program
1.2 AFirstLookat Input/Output
1.3 AWordaboutComments
1.4 FlowofControl
1.4.1 The whileStatement
1.4.2 The forStatement
1.4.3 ReadinganUnknownNumberof Inputs
1.4.4 The ifStatement
1.5 IntroducingClasses
1.5.1 The Sales_itemClass
1.5.2 AFirstLookatMemberFunctions
1.6 TheBookstoreProgram
ChapterSummary
DefinedTerms
Part I The Basics
Chapter 2 Variables and Basic Types
2.1 PrimitiveBuilt-inTypes
2.1.1 ArithmeticTypes
2.1.2 TypeConversions
2.1.3 Literals
2.2 Variables
2.2.1 VariableDefinitions
2.2.2 VariableDeclarations andDefinitions
2.2.3 Identifiers
2.2.4 Scopeof aName
2.3 CompoundTypes
2.3.1 References
2.3.2 Pointers
vii
viii Contents
2.3.3 UnderstandingCompoundTypeDeclarations
2.4 constQualifier
2.4.1 References to const
2.4.2 Pointers and const
2.4.3 Top-Level const
2.4.4 constexprandConstantExpressions
2.5 DealingwithTypes
2.5.1 TypeAliases
2.5.2 The autoTypeSpecifier
2.5.3 The decltypeTypeSpecifier
2.6 DefiningOurOwnDataStructures
2.6.1 Defining the Sales_dataType
2.6.2 Using the Sales_dataClass
2.6.3 Writing Our Own Header Files
ChapterSummary
DefinedTerms
Chapter 3 Strings, Vectors, and Arrays
3.1 Namespace usingDeclarations
3.2 Library stringType
3.2.1 Defining and Initializing strings
3.2.2 Operations on strings
3.2.3 Dealing with the Characters in a string
3.3 Library vectorType
3.3.1 Defining and Initializing vectors
3.3.2 Adding Elements to a vector
3.3.3 Other vectorOperations
3.4 IntroducingIterators
3.4.1 UsingIterators
3.4.2 IteratorArithmetic
3.5 Arrays
3.5.1 DefiningandInitializingBuilt-inArrays
3.5.2 AccessingtheElementsof anArray
3.5.3 Pointers andArrays
3.5.4 C-StyleCharacterStrings
3.5.5 InterfacingtoOlderCode
3.6 MultidimensionalArrays
ChapterSummary
DefinedTerms
Chapter 4 Expressions
4.1 Fundamentals
4.1.1 BasicConcepts
4.1.2 PrecedenceandAssociativity
4.1.3 OrderofEvaluation
4.2 ArithmeticOperators
4.3 Logical andRelationalOperators
Contents ix
4.4 AssignmentOperators
4.5 Increment andDecrementOperators
4.6 TheMemberAccessOperators
4.7 TheConditionalOperator
4.8 TheBitwiseOperators
4.9 The sizeofOperator
4.10 CommaOperator
4.11 TypeConversions
4.11.1 TheArithmeticConversions
4.11.2 Other ImplicitConversions
4.11.3 ExplicitConversions
4.12 OperatorPrecedenceTable
ChapterSummary
DefinedTerms
Chapter 5 Statements
5.1 Simple Statements
5.2 StatementScope
5.3 Conditional Statements
5.3.1 The ifStatement
5.3.2 The switchStatement
5.4 IterativeStatements
5.4.1 The whileStatement
5.4.2 Traditional forStatement
5.4.3 Range forStatement
5.4.4 The do whileStatement
5.5 JumpStatements
5.5.1 The breakStatement
5.5.2 The continueStatement
5.5.3 The gotoStatement
5.6 tryBlocks andExceptionHandling
5.6.1 A throwExpression
5.6.2 The tryBlock
5.6.3 StandardExceptions
ChapterSummary
DefinedTerms
Chapter 6 Functions
6.1 FunctionBasics
6.1.1 LocalObjects
6.1.2 FunctionDeclarations
6.1.3 SeparateCompilation
6.2 ArgumentPassing
6.2.1 PassingArgumentsbyValue
6.2.2 PassingArgumentsbyReference
6.2.3 constParametersandArguments
6.2.4 ArrayParameters
x Contents
6.2.5 main:HandlingCommand-LineOptions
6.2.6 FunctionswithVaryingParameters
6.3 Return Types and the returnStatement
6.3.1 FunctionswithNoReturnValue
6.3.2 FunctionsThatReturnaValue
6.3.3 ReturningaPointer toanArray
6.4 OverloadedFunctions
6.4.1 OverloadingandScope
6.5 Features forSpecializedUses
6.5.1 DefaultArguments
6.5.2 Inline and constexprFunctions
6.5.3 Aids for Debugging
6.6 FunctionMatching
6.6.1 ArgumentTypeConversions
6.7 Pointers toFunctions
ChapterSummary
DefinedTerms
Chapter 7 Classes
7.1 DefiningAbstractDataTypes
7.1.1 Designing the Sales_dataClass
7.1.2 Defining the Revised Sales_dataClass
7.1.3 DefiningNonmemberClass-RelatedFunctions
7.1.4 Constructors
7.1.5 Copy,Assignment, andDestruction
7.2 AccessControl andEncapsulation
7.2.1 Friends
7.3 AdditionalClassFeatures
7.3.1 ClassMembersRevisited
7.3.2 Functions That Return *this
7.3.3 ClassTypes
7.3.4 FriendshipRevisited
7.4 ClassScope
7.4.1 NameLookupandClassScope
7.5 ConstructorsRevisited
7.5.1 Constructor InitializerList
7.5.2 DelegatingConstructors
7.5.3 TheRoleof theDefaultConstructor
7.5.4 ImplicitClass-TypeConversions
7.5.5 AggregateClasses
7.5.6 LiteralClasses
7.6 staticClassMembers
ChapterSummary
DefinedTerms
Contents xi
Part II The C++ Library
Chapter 8 The IO Library
8.1 The IOClasses
8.1.1 NoCopyorAssignfor IOObjects
8.1.2 ConditionStates
8.1.3 ManagingtheOutputBuffer
8.2 File Input and Output
8.2.1 Using File Stream Objects
8.2.2 File Modes
8.3 stringStreams
8.3.1 Using an istringstream
8.3.2 Using ostringstreams
ChapterSummary
DefinedTerms
Chapter 9 Sequential Containers
9.1 Overviewof the SequentialContainers
9.2 ContainerLibraryOverview
9.2.1 Iterators
9.2.2 ContainerTypeMembers
9.2.3 begin and endMembers
9.2.4 DefiningandInitializingaContainer
9.2.5 Assignment and swap
9.2.6 ContainerSizeOperations
9.2.7 RelationalOperators
9.3 SequentialContainerOperations
9.3.1 AddingElements toaSequentialContainer
9.3.2 AccessingElements
9.3.3 ErasingElements
9.3.4 Specialized forward_listOperations
9.3.5 ResizingaContainer
9.3.6 ContainerOperationsMayInvalidateIterators
9.4 How a vectorGrows
9.5 Additional stringOperations
9.5.1 Other Ways to Construct strings
9.5.2 Other Ways to Change a string
9.5.3 stringSearchOperations
9.5.4 The compareFunctions
9.5.5 NumericConversions
9.6 ContainerAdaptors
ChapterSummary
DefinedTerms
xii Contents
Chapter 10 Generic Algorithms
10.1 Overview
10.2 AFirstLookat theAlgorithms
10.2.1 Read-OnlyAlgorithms
10.2.2 AlgorithmsThatWriteContainerElements
10.2.3 AlgorithmsThatReorderContainerElements
10.3 CustomizingOperations
10.3.1 PassingaFunctiontoanAlgorithm
10.3.2 LambdaExpressions
10.3.3 LambdaCapturesandReturns
10.3.4 BindingArguments
10.4 Revisiting Iterators
10.4.1 Insert Iterators
10.4.2 iostream Iterators
10.4.3 Reverse Iterators
10.5 StructureofGenericAlgorithms
10.5.1 TheFive IteratorCategories
10.5.2 AlgorithmParameterPatterns
10.5.3 AlgorithmNamingConventions
10.6 Container-SpecificAlgorithms
ChapterSummary
DefinedTerms
Chapter 11 Associative Containers
11.1 UsinganAssociativeContainer
11.2 Overviewof theAssociativeContainers
11.2.1 DefininganAssociativeContainer
11.2.2 Requirements onKeyType
11.2.3 The pairType
11.3 Operations onAssociativeContainers
11.3.1 AssociativeContainer Iterators
11.3.2 AddingElements
11.3.3 ErasingElements
11.3.4 Subscripting a map
11.3.5 AccessingElements
11.3.6 AWordTransformationMap
11.4 TheUnorderedContainers
ChapterSummary
DefinedTerms
Chapter 12 DynamicMemory
12.1 DynamicMemoryandSmartPointers
12.1.1 The shared_ptrClass
12.1.2 ManagingMemoryDirectly
12.1.3 Using shared_ptrs with new
12.1.4 SmartPointers andExceptions
12.1.5 unique_ptr
Contents xiii
12.1.6 weak_ptr
12.2 DynamicArrays
12.2.1 newandArrays
12.2.2 The allocatorClass
12.3 UsingtheLibrary:AText-QueryProgram
12.3.1 Designof theQueryProgram
12.3.2 DefiningtheQueryProgramClasses
ChapterSummary
DefinedTerms
Part III Tools for Class Authors
Chapter 13 Copy Control
13.1 Copy,Assign, andDestroy
13.1.1 TheCopyConstructor
13.1.2 TheCopy-AssignmentOperator
13.1.3 TheDestructor
13.1.4 TheRuleofThree/Five
13.1.5 Using = default
13.1.6 PreventingCopies
13.2 CopyControl andResourceManagement
13.2.1 ClassesThatActLikeValues
13.2.2 DefiningClassesThatActLikePointers
13.3 Swap
13.4 ACopy-ControlExample
13.5 ClassesThatManageDynamicMemory
13.6 MovingObjects
13.6.1 RvalueReferences
13.6.2 MoveConstructor andMoveAssignment
13.6.3 RvalueReferencesandMemberFunctions
ChapterSummary
DefinedTerms
Chapter 14 Overloaded Operations and Conversions
14.1 BasicConcepts
14.2 Input andOutputOperators
14.2.1 Overloading the Output Operator <<
14.2.2 Overloading the Input Operator >>
14.3 Arithmetic andRelationalOperators
14.3.1 EqualityOperators
14.3.2 RelationalOperators
14.4 AssignmentOperators
14.5 SubscriptOperator
14.6 Increment andDecrementOperators
14.7 MemberAccessOperators
14.8 Function-CallOperator
xiv Contents
14.8.1 LambdasAreFunctionObjects
14.8.2 Library-DefinedFunctionObjects
14.8.3 Callable Objects and function
14.9 Overloading,Conversions, andOperators
14.9.1 ConversionOperators
14.9.2 AvoidingAmbiguousConversions
14.9.3 FunctionMatchingandOverloadedOperators
ChapterSummary
DefinedTerms
Chapter 15 Object-Oriented Programming
15.1 OOP:AnOverview
15.2 DefiningBaseandDerivedClasses
15.2.1 DefiningaBaseClass
15.2.2 DefiningaDerivedClass
15.2.3 Conversions andInheritance
15.3 VirtualFunctions
15.4 AbstractBaseClasses
15.5 AccessControl andInheritance
15.6 ClassScopeunder Inheritance
15.7 Constructors andCopyControl
15.7.1 VirtualDestructors
15.7.2 SynthesizedCopyControl andInheritance
15.7.3 Derived-ClassCopy-ControlMembers
15.7.4 InheritedConstructors
15.8 Containers andInheritance
15.8.1 Writing a BasketClass
15.9 TextQueriesRevisited
15.9.1 AnObject-OrientedSolution
15.9.2 The Query_base and QueryClasses
15.9.3 TheDerivedClasses
15.9.4 The evalFunctions
ChapterSummary
DefinedTerms
Chapter 16 Templates and Generic Programming
16.1 DefiningaTemplate
16.1.1 FunctionTemplates
16.1.2 ClassTemplates
16.1.3 TemplateParameters
16.1.4 MemberTemplates
16.1.5 Controlling Instantiations
16.1.6 Efficiency and Flexibility
16.2 TemplateArgumentDeduction
16.2.1 Conversions andTemplateTypeParameters
16.2.2 Function-TemplateExplicitArguments
16.2.3 Trailing Return Types and Type Transformation
Contents xv
16.2.4 FunctionPointers andArgumentDeduction
16.2.5 TemplateArgumentDeductionandReferences
16.2.6 Understanding std::move
16.2.7 Forwarding
16.3 OverloadingandTemplates
16.4 VariadicTemplates
16.4.1 WritingaVariadicFunctionTemplate
16.4.2 PackExpansion
16.4.3 ForwardingParameterPacks
16.5 Template Specializations
ChapterSummary
DefinedTerms
Part IV Advanced Topics
Chapter 17 Specialized Library Facilities
17.1 The tupleType
17.1.1 Defining and Initializing tuples
17.1.2 Using a tuple toReturnMultipleValues
17.2 The bitsetType
17.2.1 Defining and Initializing bitsets
17.2.2 Operations on bitsets
17.3 RegularExpressions
17.3.1 UsingtheRegularExpressionLibrary
17.3.2 TheMatchandRegex IteratorTypes
17.3.3 UsingSubexpressions
17.3.4 Using regex_replace
17.4 RandomNumbers
17.4.1 Random-NumberEngines andDistribution
17.4.2 OtherKinds ofDistributions
17.5 The IOLibraryRevisited
17.5.1 FormattedInput andOutput
17.5.2 UnformattedInput/OutputOperations
17.5.3 RandomAccess toaStream
ChapterSummary
DefinedTerms
Chapter 18 Tools for Large Programs
18.1 ExceptionHandling
18.1.1 ThrowinganException
18.1.2 CatchinganException
18.1.3 Function tryBlocks andConstructors
18.1.4 The noexceptExceptionSpecification
18.1.5 ExceptionClassHierarchies
18.2 Namespaces
18.2.1 NamespaceDefinitions
xvi Contents
18.2.2 UsingNamespaceMembers
18.2.3 Classes,Namespaces,andScope
18.2.4 OverloadingandNamespaces
18.3 Multiple andVirtual Inheritance
18.3.1 Multiple Inheritance
18.3.2 Conversions andMultipleBaseClasses
18.3.3 ClassScopeunderMultiple Inheritance
18.3.4 Virtual Inheritance
18.3.5 Constructors andVirtual Inheritance
ChapterSummary
DefinedTerms
Chapter 19 Specialized Tools and Techniques
19.1 Controlling Memory Allocation
19.1.1 Overloading new and delete
19.1.2 Placement newExpressions
19.2 Run-TimeTypeIdentification
19.2.1 The dynamic_castOperator
19.2.2 The typeidOperator
19.2.3 UsingRTTI
19.2.4 The type_infoClass
19.3 Enumerations
19.4 Pointer toClassMember
19.4.1 Pointers toDataMembers
19.4.2 Pointers toMemberFunctions
19.4.3 UsingMemberFunctions asCallableObjects
19.5 NestedClasses
19.6 union:ASpace-SavingClass
19.7 LocalClasses
19.8 InherentlyNonportableFeatures
19.8.1 Bit-fields
19.8.2 volatileQualifier
19.8.3 Linkage Directives: extern "C"
ChapterSummary
DefinedTerms
Appendix A The Library
A.1 LibraryNames andHeaders
A.2 ABriefTourof theAlgorithms
A.2.1 Algorithms toFindanObject
A.2.2 OtherRead-OnlyAlgorithms
A.2.3 BinarySearchAlgorithms
A.2.4 AlgorithmsThatWriteContainerElements
A.2.5 PartitioningandSortingAlgorithms
A.2.6 GeneralReorderingOperations
A.2.7 PermutationAlgorithms
A.2.8 SetAlgorithms forSortedSequences
Contents xvii
A.2.9 MinimumandMaximumValues
A.2.10 NumericAlgorithms
A.3 RandomNumbers
A.3.1 RandomNumberDistributions
A.3.2 RandomNumberEngines
Index
New Features in C++
2.1.1 long longType
2.2.1 List Initialization
2.3.2 nullptrLiteral
2.4.4 constexprVariables
2.5.1 TypeAliasDeclarations
2.5.2 The autoTypeSpecifier
2.5.3 The decltypeTypeSpecifier
2.6.1 In-Class Initializers
3.2.2 Using auto or decltype forTypeAbbreviation
3.2.3 Range forStatement
3.3 Defining a vector of vectors
3.3.1 List Initialization for vectors
3.4.1 Container cbegin and cendFunctions
3.5.3 Library begin and endFunctions
3.6 Using auto or decltype to SimplifyDeclarations
4.2 RoundingRules forDivision
4.4 Assignment fromaBracedListofValues
4.9 sizeofAppliedtoaClassMember
5.4.3 Range forStatement
6.2.6 Library initializer_listClass
6.3.2 List InitializingaReturnValue
6.3.3 Declaring a Trailing Return Type
6.3.3 Using decltype to Simplify Return Type Declarations
6.5.2 constexprFunctions
7.1.4 Using = default toGenerateaDefaultConstructor
7.3.1 In-class Initializers forMembersofClassType
7.5.2 DelegatingConstructors
7.5.6 constexprConstructors
8.2.1 Using strings for File Names
9.1 The array and forward_listContainers
9.2.3 Container cbegin and cendFunctions
9.2.4 List InitializationforContainers
9.2.5 Container Nonmember swapFunctions
9.3.1 Return Type for Container insertMembers
9.3.1 Container emplaceMembers
xix
xx New Features in C++
9.4 shrink_to_fit
9.5.5 Numeric Conversion Functions for strings
10.3.2 LambdaExpressions
10.3.3 Trailing Return Type in Lambda Expressions
10.3.4 The Library bindFunction
11.2.1 List Initializationof anAssociativeContainer
11.2.3 List Initializing pairReturnType
11.3.2 List Initialization of a pair
11.4 TheUnorderedContainers
12.1 SmartPointers
12.1.1 The shared_ptrClass
12.1.2 List InitializationofDynamicallyAllocatedObjects
12.1.2 autoandDynamicAllocation
12.1.5 The unique_ptrClass
12.1.6 The weak_ptrClass
12.2.1 Range for Doesn't Apply to Dynamically Allocated Arrays
12.2.1 List InitializationofDynamicallyAllocatedArrays
12.2.1 autoCan'tBeUsedtoAllocateanArray
12.2.2 allocator::constructCanUseanyConstructor
13.1.5 Using = default forCopy-ControlMembers
13.1.6 Using = delete toPreventCopyingClassObjects
13.5 MovingInsteadofCopyingClassObjects
13.6.1 RvalueReferences
13.6.1 The Library moveFunction
13.6.2 MoveConstructor andMoveAssignment
13.6.2 Move Constructors Usually Should Be noexcept
13.6.2 MoveIterators
13.6.3 ReferenceQualifiedMemberFunctions
14.8.3 The functionClassTemplate
14.9.1 explicitConversionOperators
15.2.2 overrideSpecifier forVirtualFunctions
15.2.2 Preventing Inheritance by Defining a Class as final
15.3 override and final Specifiers for Virtual Functions
15.7.2 DeletedCopyControl andInheritance
15.7.4 InheritedConstructors
16.1.2 DeclaringaTemplateTypeParameteras aFriend
16.1.2 TemplateTypeAliases
16.1.3 DefaultTemplateArguments forTemplateFunctions
16.1.5 ExplicitControlof Instantiation
16.2.3 Template Functions and Trailing Return Types
16.2.5 ReferenceCollapsingRules
16.2.6 static_cast fromanLvaluetoanRvalue
16.2.7 The Library forwardFunction
16.4 VariadicTemplates
16.4 The sizeof...Operator
16.4.3 VariadicTemplates andForwarding
New Features in C++11 xxi
17.1 The Library TupleClassTemplate
17.2.2 New bitsetOperations
17.3 TheRegularExpressionLibrary
17.4 TheRandomNumberLibrary
17.5.1 Floating-Point FormatControl
18.1.4 The noexceptExceptionSpecifier
18.1.4 The noexceptOperator
18.2.1 InlineNamespaces
18.3.1 InheritedConstructors andMultiple Inheritance
19.3 Scoped enums
19.3 Specifying the Type Used to Hold an enum
19.3 Forward Declarations for enums
19.4.3 The Library mem_fnClassTemplate
19.6 UnionMembersofClassTypes