Cover....1
Title Page....2
Copyright and Credits....3
Contributors....4
Table of Contents....6
Preface....14
Chapter 1: New C++20 Features....24
Technical requirements....25
Format text with the new format library....25
How to do it…....26
How it works…....27
There's more…....29
Use compile-time vectors and strings with constexpr....30
How to do it…....31
How it works…....31
Safely compare integers of different types....32
How to do it…....33
How it works…....33
Use the "spaceship" operator <=> for three-way comparisons....34
How to do it…....35
How it works…....37
There's more…....37
Easily find feature test macros with the header....39
How to do it…....39
How it works…....40
Create safer templates with concepts and constraints....40
How to do it…....41
How it works…....43
There's more…....44
Avoid re-compiling template libraries with modules....46
How to do it…....46
How it works…....48
Create views into containers with ranges....51
How to do it…....52
How it works…....54
There's more…....56
Chapter 2: General STL Features....58
Technical requirements....58
Use the new span class to make your C-arrays safer....59
How to do it…....59
How it works…....60
Use structured binding to return multiple values....61
How to do it…....62
How it works…....64
Initialize variables within if and switch statements....66
How to do it…....66
How it works…....67
There's more…....68
Use template argument deduction for simplicity and clarity....69
How to do it…....69
How it works…....71
There's more…....73
Use if constexpr to simplify compile-time decisions....74
How to do it…....74
How it works…....75
Chapter 3: STL Containers....76
A quick overview of the STL container types....76
Sequential containers....76
Associative containers....77
Container adapters....78
Technical requirements....79
Use uniform erasure functions to delete items from a container....79
How to do it…....80
How it works…....82
Delete items from an unsorted vector in constant time....84
How to do it…....84
How it works…....86
Access vector elements directly and safely....87
How to do it…....87
How it works…....89
There's more…....89
Keep vector elements sorted....90
How to do it…....90
How it works…....92
There's more…....92
Efficiently insert elements into a map....93
How to do it…....94
How it works…....96
Efficiently modify the keys of map items....98
How to do it…....98
How it works…....101
There's more…....101
Use unordered_map with custom keys....102
How to do it…....102
How it works…....104
Use set to sort and filter user input....105
How to do it…....105
How it works…....106
A simple RPN calculator with deque....107
How to do it…....109
How it works…....113
There's more…....114
A word frequency counter with map....115
How to do it…....115
How it works…....119
Find long sentences with a vector of vectors....120
How to do it…....120
How it works…....123
A ToDo list using multimap....124
How to do it…....124
How it works…....126
Chapter 4: Compatible Iterators....128
Iterators are fundamental....128
Iterator categories....131
Iterator concepts....131
Technical requirements....134
Create an iterable range....135
How to do it…....135
How it works…....137
There's more…....138
Make your iterators compatible with STL iterator traits....138
How to do it…....139
How it works…....140
There's more…....140
Use iterator adapters to fill STL containers....141
How to do it…....141
How it works…....144
Create a generator as iterators....145
How to do it…....146
How it works…....148
There's more…....149
Use reverse iterator adapters to iterate backward....150
How to do it…....150
How it works…....152
Iterate objects of unknown length with a sentinel....153
How to do it…....154
How it works…....156
Build a zip iterator adapter....156
How to do it…....157
How it works…....161
There's more…....163
Create a random-access iterator....163
How to do it…....164
How it works…....169
Chapter 5: Lambda Expressions....170
Lambda expressions....170
Closures....171
Technical requirements....172
Use lambdas for scoped reusable code....173
How to do it…....173
How it works…....176
Use lambdas as predicates with the algorithm library....178
How to do it…....178
How it works…....180
Use std::function as a polymorphic wrapper....181
How to do it…....181
How it works…....183
There's more…....184
Concatenate lambdas with recursion....185
How to do it…....185
How it works…....186
Combine predicates with logical conjunction....187
How to do it…....187
How it works…....188
Call multiple lambdas with the same input....189
How to do it…....189
How it works…....190
Use mapped lambdas for a jump table....191
How to do it…....191
How it works…....192
Chapter 6: STL Algorithms....194
Technical requirements....195
Copy from one iterator to another....195
How to do it…....196
How it works…....198
Join container elements into a string....199
How to do it…....200
How it works…....202
There's more…....203
Sort containers with std::sort....203
How to do it…....204
How it works…....207
Modify containers with std::transform....208
How to do it…....208
How it works…....210
Find items in a container....211
How to do it…....211
How it works…....213
There's more…....214
Limit the values of a container to a range with std::clamp....214
How to do it…....214
How it works…....216
Sample data sets with std::sample....217
How to do it…....217
How it works…....220
Generate permutations of data sequences....220
How to do it…....221
How it works…....222
Merge sorted containers....223
How to do it…....223
How it works…....225
Chapter 7: Strings, Streams, and Formatting....226
String formatting....227
Technical requirements....228
Use string_view as a lightweight string object....228
How to do it…....228
How it works…....230
Concatenate strings....232
How to do it…....232
How it works…....234
There's more…....234
Why would I choose one over another?....238
Transform strings....238
How to do it…....239
How it works…....241
Format text with C++20's format library....241
How to do it…....242
How it works…....246
There's more…....247
Trim whitespace from strings....248
How to do it…....248
How it works…....249
Read strings from user input....250
How to do it…....251
How it works…....254
Count words in a file....254
How to do it…....254
How it works…....255
Initialize complex structures from file input....255
How to do it…....256
How it works…....258
There's more…....258
Customize a string class with char_traits....260
How to do it…....260
How it works…....263
There's more…....263
Parse strings with Regular Expressions....264
How to do it…....265
How it works…....266
Chapter 8: Utility Classes....268
Technical requirements....269
Manage optional values with std::optional....269
How to do it…....270
How it works…....272
There's more…....272
Use std::any for type safety....273
How to do it…....274
How it works…....275
Store different types with std::variant....276
Differences from the primitive union structure....276
How to do it…....277
How it works…....281
Time events with std::chrono....282
How to do it…....282
How it works…....287
Use fold expressions for variadic tuples....288
Fold expressions....288
How to do it…....290
How it works…....291
There's more…....292
Manage allocated memory with std::unique_ptr....293
How to do it…....294
How it works…....297
Share objects with std::shared_ptr....298
How to do it…....299
How it works…....302
Use weak pointers with shared objects....303
How to do it…....303
How it works…....305
There's more…....306
Share members of a managed object....307
How to do it…....307
How it works…....309
Compare random number engines....310
How to do it…....310
How it works…....314
There's more…....314
Compare random number distribution generators....315
How to do it…....315
How it works…....318
Chapter 9: Concurrency and Parallelism....320
Technical requirements....321
Sleep for a specific amount of time....321
How to do it…....321
How it works…....322
There's more…....323
Use std::thread for concurrency....323
How to do it…....323
How it works…....327
There's more…....328
Use std::async for concurrency....329
How to do it…....329
How it works…....333
Run STL algorithms in parallel with execution policies....335
How to do it…....335
How it works…....337
Share data safely with mutex and locks....338
How to do it…....338
How it works…....346
There's more…....346
Share flags and values with std::atomic....347
How to do it…....347
How it works…....349
There's more…....353
Initialize threads with std::call_once....354
How to do it…....354
How it works…....355
Use std::condition_variable to resolve the producer-consumer problem....356
How to do it…....356
How it works…....358
Implement multiple producers and consumers....360
How to do it…....360
How it works…....363
Chapter 10: Using the File System....366
Technical requirements....367
Specialize std::formatter for the path class....367
How to do it…....367
How it works…....369
Use manipulation functions with path....370
How to do it…....370
How it works…....373
List files in a directory....374
How to do it…....374
How it works…....383
There's more…....383
Search directories and files with a grep utility....384
How to do it…....385
How it works…....391
See also…....391
Rename files with regex and directory_iterator....391
How to do it…....391
How it works…....395
See also…....396
Create a disk usage counter....396
How to do it…....396
How it works…....401
Chapter 11: A Few More Ideas....402
Technical requirement....402
Create a trie class for search suggestions....403
How to do it…....403
How it works…....410
Calculate the error sum of two vectors....411
How to do it…....411
How it works…....413
There's more…....413
Build your own algorithm: split....414
How to do it…....415
How it works…....418
Leverage existing algorithms: gather....418
How to do it…....419
How it works…....422
Remove consecutive whitespace....422
How to do it…....422
How it works…....423
Convert numbers to words....424
How to do it…....425
How it works…....433
There's more…....433
Index....436
Other Books You May Enjoy....447
The C++20 STL Cookbook provides recipes to help you get the most out of the C++ STL (Standard Template Library), including new features introduced with C++20.
C++ is a rich and powerful language. Built upon C, with syntactic extensions for type safety, generic programming, and object-oriented programming, C++ is essentially a low-level language. The STL provides a broad set of higher-level classes, functions, and algorithms to make your programming job easier, more effective, and less prone to error.
I've often said that C++ is five languages cobbled into one. The formal specification includes 1) the entire C language, 2) C's cryptic-yet-powerful macro preprocessor, 3) a feature-rich class/object model, 4) a generic programming model called templates, and finally, built upon C++ classes and templates, 5) the STL.