logo资料库

Beginning C++17 From Novice to Professional(5th) 无水印原版pdf.pdf

第1页 / 共788页
第2页 / 共788页
第3页 / 共788页
第4页 / 共788页
第5页 / 共788页
第6页 / 共788页
第7页 / 共788页
第8页 / 共788页
资料共788页,剩余部分请下载后查看
Cover
Copyright
Contents
About the Authors
Introduction
1: Basic Ideas
Modern C++
Standard Libraries
C++ Program Concepts
Source Files and Header Files
Comments and Whitespace
Preprocessing Directives and Standard Library Headers
Functions
Statements
Data Input and Output
return Statements
Namespaces
Names and Keywords
Classes and Objects
Templates
Code Appearance and Programming Style
Creating an Executable
Procedural and Object-Oriented Programming
Representing Numbers
Binary Numbers
Hexadecimal Numbers
Negative Binary Numbers
Octal Values
Bi-Endian and Little-Endian Systems
Floating-Point Numbers
Representing Characters
ASCII Codes
UCS and Unicode
C++ Source Characters
Escape Sequences
Summary
2: Introducing Fundamental Types of Data
Variables, Data, and Data Types
Defining Integer Variables
Signed Integer Types
Unsigned Integer Types
Zero Initialization
Defining Variables with Fixed Values
Integer Literals
Decimal Integer Literals
Hexadecimal Literals
Octal Literals
Binary Literals
Calculations with Integers
Compound Arithmetic Expressions
Assignment Operations
The op= Assignment Operators
The sizeof Operator
Incrementing and Decrementing Integers
Postfix Increment and Decrement Operations
Defining Floating-Point Variables
Floating-Point Literals
Floating-Point Calculations
Pitfalls
Invalid Floating-Point Results
Mathematical Functions
Formatting Stream Output
Mixed Expressions and Type Conversion
Explicit Type Conversion
Old-Style Casts
Finding the Limits
Finding Other Properties of Fundamental Types
Working with Character Variables
Working with Unicode Characters
The auto Keyword
Summary
3: Working with Fundamental Data Types
Operator Precedence and Associativity
Bitwise Operators
The Bitwise Shift Operators
Shifting Signed Integers
Logical Operations on Bit Patterns
Using the Bitwise AND
Using the Bitwise OR
Using the Bitwise Complement Operator
Using the Bitwise Exclusive OR
Using the Bitwise Operators: An Example
Enumerated Data Types
Aliases for Data Types
The Lifetime of a Variable
Global Variables
Summary
4: Making Decisions
Comparing Data Values
Applying the Comparison Operators
Comparing Floating-Point Values
The if Statement
Nested if Statements
Character Classification and Conversion
The if-else Statement
Nested if-else Statements
Understanding Nested ifs
Logical Operators
Logical AND
Logical OR
Logical Negation
Combining Logical Operators
Logical Operators on Integer Operands
Logical Operators vs. Bitwise Operators
Short-Circuit Evaluation
Logical XOR
The Conditional Operator
The switch Statement
Fallthrough
Statement Blocks and Variable Scope
Initialization Statements
Summary
5: Arrays and Loops
Arrays
Using an Array
Understanding Loops
The for Loop
Avoiding Magic Numbers
Defining the Array Size with the Braced Initializer
Determining the Size of an Array
Controlling a for Loop with Floating-Point Values
More Complex for Loop Control Expressions
The Comma Operator
The Range-Based for Loop
The while Loop
The do-while Loop
Nested Loops
Skipping Loop Iterations
Breaking Out of a Loop
Indefinite Loops
Controlling a for Loop with Unsigned Integers
Arrays of Characters
Multidimensional Arrays
Initializing Multidimensional Arrays
Setting Dimensions by Default
Multidimensional Character Arrays
Allocating an Array at Runtime
Alternatives to Using an Array
Using array Containers
Accessing Individual Elements
Operations on array<>s As a Whole
Conclusion and Example
Using std::vector Containers
Deleting Elements
Example and Conclusion
Summary
6: Pointers and References
What Is a Pointer?
The Address-Of Operator
The Indirection Operator
Why Use Pointers?
Pointers to Type char
Arrays of Pointers
Constant Pointers and Pointers to Constants
Pointers and Arrays
Pointer Arithmetic
The Difference Between Pointers
Comparing Pointers
Using Pointer Notation with an Array Name
Dynamic Memory Allocation
The Stack and the Free Store
Using the new and delete Operators
Dynamic Allocation of Arrays
Multidimensional Arrays
Member Selection Through a Pointer
Hazards of Dynamic Memory Allocation
Dangling Pointers and Multiple Deallocations
Allocation/Deallocation Mismatch
Memory Leaks
Fragmentation of the Free Store
Golden Rule of Dynamic Memory Allocation
Raw Pointers and Smart Pointers
Using unique_ptr Pointers
Using shared_ptr Pointers
Understanding References
Defining References
Using a Reference Variable in a Range-Based for Loop
Summary
7: Working with Strings
A Better Class of String
Defining string Objects
Operations with String Objects
Concatenating Strings
Concatenating Strings and Characters
Concatenating Strings and Numbers
Accessing Characters in a String
Accessing Substrings
Comparing Strings
The compare() Function
Comparisons Using substr()
Searching Strings
Searching Within Substrings
Searching for Any of a Set of Characters
Searching a String Backward
Modifying a String
Inserting a String
Replacing a Substring
Removing Characters from a String
std::string vs. std::vector
Converting Strings into Numbers
String Streams
Strings of International Characters
Strings of wchar_t Characters
Objects That Contain Unicode Strings
Raw String Literals
Summary
8: Defining Functions
Segmenting Your Programs
Functions in Classes
Characteristics of a Function
Defining Functions
The Function Body
Return Values
How the return Statement Works
Function Declarations
Function Prototypes
Passing Arguments to a Function
Pass-by-Value
Passing a Pointer to a Function
Passing an Array to a Function
const Pointer Parameters
Passing a Multidimensional Array to a Function
Pass-by-Reference
References vs. Pointers
Input vs. Output Parameters
Passing Arrays by Reference
References and Implicit Conversions
String Views: The New Reference-to-const-string
Using String View Function Parameters
Default Argument Values
Multiple Default Parameter Values
Arguments to main()
Returning Values from a Function
Returning a Pointer
Returning a Reference
Returning vs. Output Parameters
Return Type Deduction
Return Type Deduction and References
Working with Optional Values
std::optional
Static Variables
Inline Functions
Function Overloading
Overloading and Pointer Parameters
Overloading and Reference Parameters
Overloading and const Parameters
Overloading with const Pointer Parameters
Overloading and Reference-to-const Parameters
Overloading and Default Argument Values
Recursion
Basic Examples
Recursive Algorithms
The Quicksort Algorithm
The main() Function
The extract_words() Function
The swap() Function
The sort() Functions
The max_word_length() Function
The show_words() Function
Summary
9: Function Templates
Function Templates
Creating Instances of a Function Template
Template Type Parameters
Explicit Template Arguments
Function Template Specialization
Function Templates and Overloading
Function Templates with Multiple Parameters
Return Type Deduction for Templates
decltype() and Trailing Return Types
decltype(auto) and decltype() vs. auto
Default Values for Template Parameters
Nontype Template Parameters
Templates for Functions with Fixed-Size Array Arguments
Summary
10: Program Files and Preprocessing Directives
Understanding Translation Units
The One Definition Rule
Program Files and Linkage
Determining Linkage for a Name
External Functions
External Variables
const Variables with External Linkage
Internal Names
Preprocessing Your Source Code
Defining Preprocessor Macros
Defining Function-Like Macros
Preprocessor Operators
Undefining Macros
Including Header Files
Preventing Duplication of Header File Contents
Your First Header File
Namespaces
The Global Namespace
Defining a Namespace
Applying using Declarations
Functions and Namespaces
Unnamed Namespaces
Nested Namespaces
Namespace Aliases
Logical Preprocessing Directives
The Logical #if Directive
Testing for Specific Identifier Values
Multiple-Choice Code Selection
Standard Preprocessing Macros
Testing for Available Headers
Debugging Methods
Integrated Debuggers
Preprocessing Directives in Debugging
Using the assert() Macro
Switching Off assert() Macros
Static Assertions
Summary
11: Defining Your Own Data Types
Classes and Object-Oriented Programming
Encapsulation
Data Hiding
Inheritance
Polymorphism
Terminology
Defining a Class
Constructors
Default Constructors
Defining a Class Constructor
Using the default Keyword
Defining Functions and Constructors Outside the Class
Default Constructor Parameter Values
Using a Member Initializer List
Using the explicit Keyword
Delegating Constructors
The Copy Constructor
Implementing the Copy Constructor
Accessing Private Class Members
The this Pointer
Returning this from a Function
const Objects and const Member Functions
const Member Functions
const Correctness
Overloading on const
Casting Away const
Using the mutable Keyword
Friends
The Friend Functions of a Class
Friend Classes
Arrays of Class Objects
The Size of a Class Object
Static Members of a Class
Static Member Variables
Accessing Static Member Variables
Static Constants
Static Member Variables of the Class Type
Static Member Functions
Destructors
Using Pointers as Class Members
The Truckload Example
Defining the Package Class
Defining the Truckload Class
Traversing the Boxes Contained in a Truckload
Adding and Removing Boxes
Putting It All Together
Nested Classes
Nested Classes with Public Access
A Better Mechanism for Traversing a Truckload: Iterators
Summary
12: Operator Overloading
Implementing Operators for a Class
Operator Overloading
Implementing an Overloaded Operator
Nonmember Operator Functions
Implementing Full Support for an Operator
Implementing All Comparison Operators in a Class
Operators That Can Be Overloaded
Restrictions and Key Guideline
Operator Function Idioms
Overloading the << Operator for Output Streams
Overloading the Arithmetic Operators
Implementing One Operator in Terms of Another
Member vs. Nonmember Functions
Operator Functions and Implicit Conversions
Overloading Unary Operators
Overloading the Increment and Decrement Operators
Overloading the Subscript Operator
Modifying the Result of an Overloaded Subscript Operator
Function Objects
Overloading Type Conversions
Potential Ambiguities with Conversions
Overloading the Assignment Operator
Implementing the Copy Assignment Operator
Copy Assignment vs. Copy Construction
Assigning Different Types
Summary
13: Inheritance
Classes and Object-Oriented Programming
Hierarchies
Inheritance in Classes
Inheritance vs. Aggregation
Deriving Classes
protected Members of a Class
The Access Level of Inherited Class Members
Access Specifiers and Class Hierarchies
Choosing Access Specifiers in Class Hierarchies
Changing the Access Specification of Inherited Members
Constructors in a Derived Class
The Copy Constructor in a Derived Class
The Default Constructor in a Derived Class
Inheriting Constructors
Destructors Under Inheritance
The Order in Which Destructors Are Called
Duplicate Member Variable Names
Duplicate Member Function Names
Multiple Inheritance
Multiple Base Classes
Inherited Member Ambiguity
Repeated Inheritance
Virtual Base Classes
Converting Between Related Class Types
Summary
14: Polymorphism
Understanding Polymorphism
Using a Base Class Pointer
Calling Inherited Functions
Virtual Functions
Requirements for Virtual Function Operation
Using override
Using final
Virtual Functions and Class Hierarchies
Access Specifiers and Virtual Functions
Default Argument Values in Virtual Functions
Using References to Call Virtual Functions
Polymorphic Collections
Destroying Objects Through a Pointer
Virtual Destructors
Converting Between Pointers to Class Objects
Dynamic Casts
Casting Pointers Dynamically
Converting References
Calling the Base Class Version of a Virtual Function
Calling Virtual Functions from Constructors or Destructors
The Cost of Polymorphism
Determining the Dynamic Type
Pure Virtual Functions
Abstract Classes
Abstract Classes as Interfaces
Summary
15: Runtime Errors and Exceptions
Handling Errors
Understanding Exceptions
Throwing an Exception
The Exception-Handling Process
Code That Causes an Exception to Be Thrown
Nested try Blocks
How It Works
Class Objects as Exceptions
Matching a Catch Handler to an Exception
How It Works
Catching Derived Class Exceptions with a Base Class Handler
Rethrowing Exceptions
Unhandled Exceptions
Catching All Exceptions
Functions That Don't Throw Exceptions
The noexcept Specifier
Exceptions and Destructors
Exceptions and Resource Leaks
Resource Acquisition Is Initialization
Standard RAII Classes for Dynamic Memory
Standard Library Exceptions
The Exception Class Definitions
Using Standard Exceptions
Throwing Standard Exceptions Directly
Deriving Your Own Exception Classes
Summary
16: Class Templates
Understanding Class Templates
Defining Class Templates
Template Parameters
A Simple Class Template
Defining Member Functions of a Class Template
Constructor Templates
The Destructor Template
Subscript Operator Templates
The Assignment Operator Template
Instantiating a Class Template
Class Template Argument Deduction
Nontype Class Template Parameters
Templates for Member Functions with Nontype Parameters
Arguments for Nontype Parameters
Nontype Template Arguments vs. Constructor Arguments
Default Values for Template Parameters
Explicit Template Instantiation
Class Template Specialization
Defining a Class Template Specialization
Partial Template Specialization
Choosing Between Multiple Partial Specializations
Using static_assert() in a Class Template
Friends of Class Templates
Class Templates with Nested Classes
Function Templates for Stack Members
Disambiguating Dependent Names
Summary
17: Move Semantics
Lvalues and Rvalues
Rvalue References
Moving Objects
Traditional Workarounds
Defining Move Members
Move Constructors
Move Assignment Operators
Explicitly Moved Objects
Move-Only Types
Extended Use of Moved Objects
A Barrel of Contradictions
std::move() Does Not Move
An Rvalue Reference Is an Lvalue
Defining Functions Revisited
Pass-by-Rvalue-Reference
The Return of Pass-by-Value
Return-by-Value
Defining Move Members Revisited
Always Add noexcept
Moving Within Standard Library Containers
The "Move-and-Swap" Idiom
Special Member Functions
Default Move Members
The Rule of Five
The Rule of Zero
Summary
18: First-Class Functions
Pointers to Functions
Defining Pointers to Functions
Callback Functions for Higher-Order Functions
Type Aliases for Function Pointers
Function Objects
Basic Function Objects
Standard Function Objects
Parameterized Function Objects
Lambda Expressions
Defining a Lambda Expression
Naming a Lambda Closure
Passing a Lambda Expression to a Function Template
The Capture Clause
Capturing by Value
Capturing by Reference
Capturing Specific Variables
Capturing the this Pointer
The std::function<> Template
Summary
19: Containers and Algorithms
Containers
Sequence Containers
Arrays
Lists
Key Operations
Stacks and Queues
LIFO vs. FIFO Semantics
Priority Queues
Sets
Ordered Sets
Unordered Sets
Maps
Elements of a Map
Counting Words
Iterators
The Iterator Design Pattern
Iterators for Standard Library Containers
Creating and Working with Standard Iterators
Different Flavors of Iterators
Traversing Elements of a Container
Const Iterators
Inserting in and Erasing from Sequence Containers
Altering Containers During Iteration
Iterators for Arrays
Algorithms
A First Example
Finding Elements
Outputting Multiple Values
The Remove-Erase Idiom
Sorting
Parallel Algorithms
Summary
Index
分享到:
收藏