c 语言导向
Contents
Preface xi
1 The Basics 1
1.1 Introduction ................................................................................. 1
1.2 Programs ..................................................................................... 2
1.3 Functions ..................................................................................... 4
1.4 Types, Variables, and Arithmetic ................................................ 5
1.5 Scope and Lifetime ..................................................................... 9
1.6 Constants ..................................................................................... 9
1.7 Pointers, Arrays, and References ................................................ 11
1.8 Tests ............................................................................................ 14
1.9 Mapping to Hardware ................................................................. 16
1.10 Advice ......................................................................................... 18
2 User-Defined Types 21
2.1 Introduction ................................................................................. 21
2.2 Structures .................................................................................... 22
2.3 Classes ........................................................................................ 23
2.4 Unions ......................................................................................... 25
2.5 Enumerations .............................................................................. 26
2.6 Advice ......................................................................................... 27
vi Contents
3 Modularity 29
3.1 Introduction ................................................................................. 29
3.2 Separate Compilation .................................................................. 30
3.3 Modules (C 20) ........................................................................ 32
3.4 Namespaces ................................................................................. 34
3.5 Error Handling ............................................................................ 35
3.6 Function Arguments and Return Values ..................................... 36
3.7 Advice ......................................................................................... 46
4 Classes 47
4.1 Introduction ................................................................................. 47
4.2 Concrete Types ............................................................................ 48
4.3 Abstract Types ............................................................................ 54
4.4 Virtual Functions ......................................................................... 56
4.5 Class Hierarchies ........................................................................ 57
4.6 Advice ......................................................................................... 63
5 Essential Operations 65
5.1 Introduction ................................................................................. 65
5.2 Copy and Move ........................................................................... 68
5.3 Resource Management ................................................................ 72
5.4 Conventional Operations ............................................................. 74
5.5 Advice ......................................................................................... 77
6 Templates 79
6.1 Introduction ................................................................................. 79
6.2 Parameterized Types ................................................................... 79
6.3 Parameterized Operations ........................................................... 84
6.4 Template Mechanisms ................................................................ 89
6.5 Advice ......................................................................................... 92
7 Concepts and Generic Programming 93
7.1 Introduction ................................................................................. 93
7.2 Concepts ...................................................................................... 94
7.3 Generic Programming ................................................................. 98
7.4 Variadic Templates ...................................................................... 100
7.5 Template Compilation Model ..................................................... 104
7.6 Advice ......................................................................................... 104
vii
8 Library Overview 107
8.1 Introduction ................................................................................. 107
8.2 Standard-Library Components .................................................... 108
8.3 Standard-Library Headers and Namespace ................................. 109
8.4 Advice ......................................................................................... 110
9 Strings and Regular Expressions 111
9.1 Introduction ................................................................................. 111
9.2 Strings ......................................................................................... 111
9.3 String Views ................................................................................ 114
9.4 Regular Expressions .................................................................... 116
9.5 Advice ......................................................................................... 122
10 Input and Output 123
10.1 Introduction ................................................................................. 123
10.2 Output ......................................................................................... 123
10.3 Input ............................................................................................ 125
10.4 I/O State ...................................................................................... 127
10.5 I/O of User-Defined Types .......................................................... 128
10.6 Formatting ................................................................................... 129
10.7 File Streams ................................................................................ 130
10.8 String Streams ............................................................................. 130
10.9 C-style I/O ................................................................................... 131
10.10 File System ................................................................................. 132
10.11 Advice ......................................................................................... 136
11 Containers 137
11.1 Introduction ................................................................................. 137
11.2 vector ........................................................................................... 138
11.3 list ................................................................................................ 142
11.4 map .............................................................................................. 144
11.5 unordered_map ............................................................................ 144
11.6 Container Overview .................................................................... 146
11.7 Advice ......................................................................................... 148
12 Algorithms 149
12.1 Introduction ................................................................................. 149
12.2 Use of Iterators ............................................................................ 150
12.3 Iterator Types .............................................................................. 153
viii Contents
12.4 Stream Iterators ........................................................................... 154
12.5 Predicates .................................................................................... 155
12.6 Algorithm Overview ................................................................... 156
12.7 Concepts (C 20) ....................................................................... 157
12.8 Container Algorithms ................................................................. 160
12.9 Parallel Algorithms ..................................................................... 161
12.10 Advice ......................................................................................... 161
13 Utilities 163
13.1 Introduction ................................................................................. 163
13.2 Resource Management ................................................................ 164
13.3 Range Checking: span ................................................................ 168
13.4 Specialized Containers ................................................................ 170
13.5 Alternatives ................................................................................. 174
13.6 Allocators .................................................................................... 178
13.7 Time ............................................................................................ 179
13.8 Function Adaption ...................................................................... 180
13.9 Type Functions ............................................................................ 181
13.10 Advice ......................................................................................... 185
14 Numerics 187
14.1 Introduction ................................................................................. 187
14.2 Mathematical Functions .............................................................. 188
14.3 Numerical Algorithms ................................................................ 189
14.4 Complex Numbers ...................................................................... 190
14.5 Random Numbers ....................................................................... 191
14.6 Vector Arithmetic ........................................................................ 192
14.7 Numeric Limits ........................................................................... 193
14.8 Advice ......................................................................................... 193
15 Concurrency 195
15.1 Introduction ................................................................................. 195
15.2 Tasks and thread s ........................................................................ 196
15.3 Passing Arguments ...................................................................... 197
15.4 Returning Results ........................................................................ 198
15.5 Sharing Data ................................................................................ 199
15.6 Waiting for Events ...................................................................... 200
15.7 Communicating Tasks ................................................................. 202
15.8 Advice ......................................................................................... 205
ix
16 History and Compatibility 207
16.1 History ........................................................................................ 207
16.2 C Feature Evolution ................................................................ 214
16.3 C/C Compatibility .................................................................. 218
16.4 Bibliography ............................................................................... 222
16.5 Advice ......................................................................................... 225
Index 227
评论