logo资料库

Discovering Modern C++.pdf

第1页 / 共2249页
第2页 / 共2249页
第3页 / 共2249页
第4页 / 共2249页
第5页 / 共2249页
第6页 / 共2249页
第7页 / 共2249页
第8页 / 共2249页
资料共2249页,剩余部分请下载后查看
About This E-Book
Title Page
Copyright Page
Dedication Page
Contents
Preface
Reasons to Learn C++
Reasons to Read This Book
The Beauty and the Beast
Languages in Science and Engineering
Typographical Conventions
Acknowledgments
About the Author
Chapter 1. C++ Basics
1.1 Our First Program
1.2 Variables
1.2.1 Constants
1.2.2 Literals
1.2.3 Non-narrowing Initialization
1.2.4 Scopes
1.3 Operators
1.3.1 Arithmetic Operators
1.3.2 Boolean Operators
1.3.3 Bitwise Operators
1.3.4 Assignment
1.3.5 Program Flow
1.3.6 Memory Handling
1.3.7 Access Operators
1.3.8 Type Handling
1.3.9 Error Handling
1.3.10 Overloading
1.3.11 Operator Precedence
1.3.12 Avoid Side Effects!
1.4 Expressions and Statements
1.4.1 Expressions
1.4.2 Statements
1.4.3 Branching
1.4.4 Loops
1.4.5 goto
1.5 Functions
1.5.1 Arguments
1.5.2 Returning Results
1.5.3 Inlining
1.5.4 Overloading
1.5.5 main Function
1.6 Error Handling
1.6.1 Assertions
1.6.2 Exceptions
1.6.3 Static Assertions
1.7 I/O
1.7.1 Standard Output
1.7.2 Standard Input
1.7.3 Input/Output with Files
1.7.4 Generic Stream Concept
1.7.5 Formatting
1.7.6 Dealing with I/O Errors
1.8 Arrays, Pointers, and References
1.8.1 Arrays
1.8.2 Pointers
1.8.3 Smart Pointers
1.8.4 References
1.8.5 Comparison between Pointers and References
1.8.6 Do Not Refer to Outdated Data!
1.8.7 Containers for Arrays
1.9 Structuring Software Projects
1.9.1 Comments
1.9.2 Preprocessor Directives
1.10 Exercises
1.10.1 Age
1.10.2 Arrays and Pointers
1.10.3 Read the Header of a Matrix Market File
Chapter 2. Classes
2.1 Program for Universal Meaning Not for Technical Details
2.2 Members
2.2.1 Member Variables
2.2.2 Accessibility
2.2.3 Access Operators
2.2.4 The Static Declarator for Classes
2.2.5 Member Functions
2.3 Setting Values: Constructors and Assignments
2.3.1 Constructors
2.3.2 Assignment
2.3.3 Initializer Lists
2.3.4 Uniform Initialization
2.3.5 Move Semantics
2.4 Destructors
2.4.1 Implementation Rules
2.4.2 Dealing with Resources Properly
2.5 Method Generation Résumé
2.6 Accessing Member Variables
2.6.1 Access Functions
2.6.2 Subscript Operator
2.6.3 Constant Member Functions
2.6.4 Reference-Qualified Members
2.7 Operator Overloading Design
2.7.1 Be Consistent!
2.7.2 Respect the Priority
2.7.3 Member or Free Function
2.8 Exercises
2.8.1 Polynomial
2.8.2 Move Assignment
2.8.3 Initializer List
2.8.4 Resource Rescue
Chapter 3. Generic Programming
3.1 Function Templates
3.1.1 Instantiation
3.1.2 Parameter Type Deduction
3.1.3 Dealing with Errors in Templates
3.1.4 Mixing Types
3.1.5 Uniform Initialization
3.1.6 Automatic return Type
3.2 Namespaces and Function Lookup
3.2.1 Namespaces
3.2.2 Argument-Dependent Lookup
3.2.3 Namespace Qualification or ADL
3.3 Class Templates
3.3.1 A Container Example
3.3.2 Designing Uniform Class and Function Interfaces
3.4 Type Deduction and Definition
3.4.1 Automatic Variable Type
3.4.2 Type of an Expression
3.4.3 decltype(auto)
3.4.4 Defining Types
3.5 A Bit of Theory on Templates: Concepts
3.6 Template Specialization
3.6.1 Specializing a Class for One Type
3.6.2 Specializing and Overloading Functions
3.6.3 Partial Specialization
3.6.4 Partially Specializing Functions
3.7 Non-Type Parameters for Templates
3.8 Functors
3.8.1 Function-like Parameters
3.8.2 Composing Functors
3.8.3 Recursion
3.8.4 Generic Reduction
3.9 Lambda
3.9.1 Capture
3.9.2 Capture by Value
3.9.3 Capture by Reference
3.9.4 Generalized Capture
3.9.5 Generic Lambdas
3.10 Variadic Templates
3.11 Exercises
3.11.1 String Representation
3.11.2 String Representation of Tuples
3.11.3 Generic Stack
3.11.4 Iterator of a Vector
3.11.5 Odd Iterator
3.11.6 Odd Range
3.11.7 Stack of bool
3.11.8 Stack with Custom Size
3.11.9 Deducing Non-type Template Arguments
3.11.10 Trapezoid Rule
3.11.11 Functor
3.11.12 Lambda
3.11.13 Implement make_unique
Chapter 4. Libraries
4.1 Standard Template Library
4.1.1 Introductory Example
4.1.2 Iterators
4.1.3 Containers
4.1.4 Algorithms
4.1.5 Beyond Iterators
4.2 Numerics
4.2.1 Complex Numbers
4.2.2 Random Number Generators
4.3 Meta-programming
4.3.1 Limits
4.3.2 Type Traits
4.4 Utilities
4.4.1 Tuple
4.4.2 function
4.4.3 Reference Wrapper
4.5 The Time Is Now
4.6 Concurrency
4.7 Scientific Libraries Beyond the Standard
4.7.1 Other Arithmetics
4.7.2 Interval Arithmetic
4.7.3 Linear Algebra
4.7.4 Ordinary Differential Equations
4.7.5 Partial Differential Equations
4.7.6 Graph Algorithms
4.8 Exercises
4.8.1 Sorting by Magnitude
4.8.2 STL Container
4.8.3 Complex Numbers
Chapter 5. Meta-Programming
5.1 Let the Compiler Compute
5.1.1 Compile-Time Functions
5.1.2 Extended Compile-Time Functions
5.1.3 Primeness
5.1.4 How Constant Are Our Constants?
5.2 Providing and Using Type Information
5.2.1 Type Traits
5.2.2 Conditional Exception Handling
5.2.3 A const-Clean View Example
5.2.4 Standard Type Traits
5.2.5 Domain-Specific Type Properties
5.2.6 enable_if
5.2.7 Variadic Templates Revised
5.3 Expression Templates
5.3.1 Simple Operator Implementation
5.3.2 An Expression Template Class
5.3.3 Generic Expression Templates
5.4 Meta-Tuning: Write Your Own Compiler Optimization
5.4.1 Classical Fixed-Size Unrolling
5.4.2 Nested Unrolling
5.4.3 Dynamic Unrolling–Warm-up
5.4.4 Unrolling Vector Expressions
5.4.5 Tuning an Expression Template
5.4.6 Tuning Reduction Operations
5.4.7 Tuning Nested Loops
5.4.8 Tuning Résumé
5.5 Exercises
5.5.1 Type Traits
5.5.2 Fibonacci Sequence
5.5.3 Meta-Program for Greatest Common Divisor
5.5.4 Vector Expression Template
5.5.5 Meta-List
Chapter 6. Object-Oriented Programming
6.1 Basic Principles
6.1.1 Base and Derived Classes
6.1.2 Inheriting Constructors
6.1.3 Virtual Functions and Polymorphic Classes
6.1.4 Functors via Inheritance
6.2 Removing Redundancy
6.3 Multiple Inheritance
6.3.1 Multiple Parents
6.3.2 Common Grandparents
6.4 Dynamic Selection by Sub-typing
6.5 Conversion
6.5.1 Casting between Base and Derived Classes
6.5.2 const-Cast
6.5.3 Reinterpretation Cast
6.5.4 Function-Style Conversion
6.5.5 Implicit Conversions
6.6 CRTP
6.6.1 A Simple Example
6.6.2 A Reusable Access Operator
6.7 Exercises
6.7.1 Non-redundant Diamond Shape
6.7.2 Inheritance Vector Class
6.7.3 Clone Function
Chapter 7. Scientific Projects
7.1 Implementation of ODE Solvers
7.1.1 Ordinary Differential Equations
7.1.2 Runge-Kutta Algorithms
7.1.3 Generic Implementation
7.1.4 Outlook
7.2 Creating Projects
7.2.1 Build Process
7.2.2 Build Tools
7.2.3 Separate Compilation
7.3 Some Final Words
Appendix A. Clumsy Stuff
A.1 More Good and Bad Scientific Software
A.2 Basics in Detail
A.2.1 More about Qualifying Literals
A.2.2 static Variables
A.2.3 More about if
A.2.4 Duff’s Device
A.2.5 More about main
A.2.6 Assertion or Exception?
A.2.7 Binary I/O
A.2.8 C-Style I/O
A.2.9 Garbarge Collection
A.2.10 Trouble with Macros
A.3 Real-World Example: Matrix Inversion
A.4 Class Details
A.4.1 Pointer to Member
A.4.2 More Initialization Examples
A.4.3 Accessing Multi-dimensional Arrays
A.5 Method Generation
A.5.1 Controlling the Generation
A.5.2 Generation Rules
A.5.3 Pitfalls and Design Guides
A.6 Template Details
A.6.1 Uniform Initialization
A.6.2 Which Function Is Called?
A.6.3 Specializing for Specific Hardware
A.6.4 Variadic Binary I/O
A.7 Using std::vector in C++03
A.8 Dynamic Selection in Old Style
A.9 Meta-Programming Details
A.9.1 First Meta-Program in History
A.9.2 Meta-Functions
A.9.3 Backward-Compatible Static Assertion
A.9.4 Anonymous Type Parameters
A.9.5 Benchmark Sources of Dynamic Unrolling
A.9.6 Benchmark for Matrix Product
Appendix B. Programming Tools
B.1 gcc
B.2 Debugging
B.2.1 Text-Based Debugger
B.2.2 Debugging with Graphical Interface: DDD
B.3 Memory Analysis
B.4 gnuplot
B.5 Unix, Linux, and Mac OS
Appendix C. Language Definitions
C.1 Value Categories
C.2 Operator Overview
C.3 Conversion Rules
C.3.1 Promotion
C.3.2 Other Conversions
C.3.3 Usual Arithmetic Conversions
C.3.4 Narrowing
Bibliography
Index
Code Snippets
About This E-Book EPUB is an open, industry-standard format for e-books. However, support for EPUB and its many features varies across reading devices and applications. Use your device or app settings to customize the presentation to your liking. Settings that you can customize often include font, font size, single or double column, landscape or portrait mode, and figures that you can click or tap to enlarge. For additional information about the settings and features on your reading device or app, visit the device manufacturer’s Web site. Many titles include programming code or configuration examples. To optimize the presentation of these elements, view the e-book in single-column, landscape mode and adjust the font size to the smallest setting. In addition to presenting code and configurations in the reflowable text format, we have included images of the code that mimic the presentation found in the print book; therefore, where the reflowable format may compromise the presentation of the code listing, you will see a “Click here to view code image” link. Click the link to view the print-fidelity code image. To return to the previous page viewed, click the Back button on your device or app.
Discovering Modern C++ An Intensive Course for Scientists, Engineers, and Programmers Peter Gottschling Boston • Columbus • Indianapolis • New York • San Francisco • Amsterdam • Cape Town Dubai • London • Madrid • Milan • Munich • Paris • Montreal • Toronto • Delhi • Mexico Sao Paulo • Sidney • Hong Kong • Seoul • Singapore • Taipei • Tokyo City
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at corpsales@pearsoned.com or (800) 382-3419. For government sales inquiries, please contact governmentsales@pearsoned.com. For questions about sales outside the U.S., please contact international@pearsoned.com. Visit us on the Web: informit.com/aw Library of Congress Control Number: 2015955339 Copyright © 2016 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/. ISBN-13: 978-0-13-438358-3 ISBN-10: 0-13-438358-3 Text printed in the United States on recycled paper at Edwards Brothers Malloy in Ann Arbor, Michigan. First printing, December 2015
To my parents, Helga and Hans-Werner
Contents Preface Reasons to Learn C++ Reasons to Read This Book The Beauty and the Beast Languages in Science and Engineering Typographical Conventions Acknowledgments About the Author Chapter 1 C++ Basics 1.1 Our First Program 1.2 Variables 1.2.1 Constants 1.2.2 Literals 1.2.3 Non-narrowing Initialization 1.2.4 Scopes 1.3 Operators 1.3.1 Arithmetic Operators 1.3.2 Boolean Operators 1.3.3 Bitwise Operators 1.3.4 Assignment 1.3.5 Program Flow 1.3.6 Memory Handling 1.3.7 Access Operators 1.3.8 Type Handling 1.3.9 Error Handling 1.3.10 Overloading 1.3.11 Operator Precedence 1.3.12 Avoid Side Effects! 1.4 Expressions and Statements 1.4.1 Expressions
1.4.2 Statements 1.4.3 Branching 1.4.4 Loops 1.4.5 goto 1.5 Functions 1.5.1 Arguments 1.5.2 Returning Results 1.5.3 Inlining 1.5.4 Overloading 1.5.5 main Function 1.6 Error Handling 1.6.1 Assertions 1.6.2 Exceptions 1.6.3 Static Assertions 1.7 I/O 1.7.1 Standard Output 1.7.2 Standard Input 1.7.3 Input/Output with Files 1.7.4 Generic Stream Concept 1.7.5 Formatting 1.7.6 Dealing with I/O Errors 1.8 Arrays, Pointers, and References 1.8.1 Arrays 1.8.2 Pointers 1.8.3 Smart Pointers 1.8.4 References 1.8.5 Comparison between Pointers and References 1.8.6 Do Not Refer to Outdated Data! 1.8.7 Containers for Arrays 1.9 Structuring Software Projects 1.9.1 Comments 1.9.2 Preprocessor Directives
分享到:
收藏