About the Author ....................................................................................................................... xi
About the Technical Reviewer ...................................................................................................... xiii
Acknowledgments .......................................................................................................................xv
Introduction ...............................................................................................................................xvii
Prologue ....................................................................................................................................xxi
Chapter 1: C++ Review .............................................................................................................. 1
Templates ................................................................................................................................. 1
Source Code Overview ............................................................................................................... 11
Containers and Iterators ............................................................................................................. 12
Strings ..................................................................................................................................... 17
User-Defined Classes .................................................................................................................. 23
User-Defined Template Classes .................................................................................................. 34
Lambda Expressions ................................................................................................................... 41
Three-Way Comparison Operator ................................................................................................ 48
Exceptions ................................................................................................................................... 58
Summary .................................................................................................................................... 61
Chapter 2: Formatted I/O .......................................................................................................... 63
Formatted Output Using std::printf( ) .......................................................................................... 63
Formatted Output Using Streams ................................................................................................ 68
Formatted Output Using std::format( ) ........................................................................................ 80
Formatted Output Using std::print( ) ............................................................................................ 93
Formatted File I/O Using Streams ............................................................................................... 97
Summary .................................................................................................................................. 103
Chapter 3: Sequence Containers – Part 1 ...................................................................................... 105
Overview of Sequence Containers ............................................................................................ 105
Using std::array ......................................................................................................................... 106
Using std::vector ....................................................................................................................... 116
More Algorithms Using std::array and std::vector ..................................................................... 128
Summary .................................................................................................................................. 134
Chapter 4: Sequence Containers – Part 2 ...................................................................................... 137
Using std::deque ....................................................................................................................... 137
Using std::list ............................................................................................................................ 148
Using std::forward_list .............................................................................................................. 158
Understanding Iterators ............................................................................................................ 165
Summary .................................................................................................................................. 171
Chapter 5: General Utilities Library ............................................................................................ 173
Heterogeneous Containers ........................................................................................................ 173
Using std::pair ..................................................................................................................... 174
Using std::tuple .................................................................................................................... 180
Utility Classes ............................................................................................................................ 190
Using std::variant ................................................................................................................. 190
Using std::optional ............................................................................................................... 200
Using std::any ...................................................................................................................... 219
Using std::expected ............................................................................................................. 223
Summary .................................................................................................................................. 231
Chapter 6: Smart Pointers ........................................................................................................ 233
Smart Pointer Primer ................................................................................................................ 233
Using std::unique_ptr ................................................................................................................ 235
Using std::shared_ptr ................................................................................................................ 248
Using std::weak_ptr .................................................................................................................. 254
Summary .................................................................................................................................. 258
Chapter 7: Associative Containers ................................................................................................ 259
Using std::set ............................................................................................................................ 259
Using std::multiset .................................................................................................................... 271
Using std::map .......................................................................................................................... 276
Using std::multimap .................................................................................................................. 292
Summary .................................................................................................................................. 300
Chapter 8: Unordered Associative Containers ............................................................................... 301
Unordered Associative Container Primer ...................................................................................... 301
Hash Functions and Concepts ..................................................................................................... 302
Using std::unordered_set .......................................................................................................... 303
Using std::unordered_multiset .................................................................................................. 326
Using std::unordered_map ........................................................................................................ 333
Using std::unordered_multimap ............................................................................................... 345
Summary .................................................................................................................................. 352
Chapter 9: Container Adaptors ................................................................................................... 355
Container Adaptor Primer .......................................................................................................... 355
Using std::stack ........................................................................................................................ 357
Using std::queue ....................................................................................................................... 370
Using std::priority_queue .......................................................................................................... 375
Flat Container Adaptors ............................................................................................................ 387
Summary .................................................................................................................................. 388
Chapter 10: Algorithms – Part 1 ................................................................................................... 391
Algorithm Primer ....................................................................................................................... 391
Counting Algorithms .................................................................................................................. 393
Minimum and Maximum Algorithms ............................................................................................. 408
Copy Algorithms ........................................................................................................................ 414
Move Algorithms ....................................................................................................................... 420
Reversal Algorithms .................................................................................................................. 425
Replacement Algorithms ........................................................................................................... 428
Removal Algorithms .................................................................................................................. 436
Fill Algorithms ........................................................................................................................... 443
Summary .................................................................................................................................. 447
Chapter 11: Algorithms – Part 2 .................................................................................................. 449
For_Each Algorithms ................................................................................................................. 449
Transformation Algorithms ........................................................................................................ 454
Generation Algorithms .............................................................................................................. 459
Find Algorithms ......................................................................................................................... 464
Contains Algorithms .................................................................................................................. 483
More Find Algorithms ................................................................................................................ 491
Search Algorithms ..................................................................................................................... 494
Accumulate and Fold Algorithms .............................................................................................. 503
Summary .................................................................................................................................. 511
Chapter 12: Algorithms – Part 3 .................................................................................................. 513
Sorting Algorithms .................................................................................................................... 513
Binary Search Algorithms ......................................................................................................... 523
Partition Algorithms .................................................................................................................. 531
Heap Algorithms ........................................................................................................................ 535
Summary .................................................................................................................................. 542
Chapter 13: Algorithms – Part 4 .................................................................................................. 545
Merge Algorithms ...................................................................................................................... 545
Shuffle and Sample Algorithms ................................................................................................ 553
Rotate and Shift Algorithms ...................................................................................................... 557
Set Algorithms ........................................................................................................................... 564
Permutation Algorithms ............................................................................................................ 568
Summary .................................................................................................................................. 572
Chapter 14: Ranges – Part 1 .................................................................................................... 575
Range Views and Adaptors Primer ............................................................................................ 575
Range Views and Adaptors ....................................................................................................... 577
Range Adaptors and Pipelines .................................................................................................. 584
Range Projections ..................................................................................................................... 590
More Range Adaptors ................................................................................................................ 598
Range Views and Adaptors Minutiae ......................................................................................... 604
Summary .................................................................................................................................. 609
Chapter 15: Ranges – Part 2 ........................................................................................................ 611
Tuple Views ............................................................................................................................... 611
More Tuple Views ...................................................................................................................... 616
Join, Split, and Cartesian Product Views ................................................................................... 626
Slide, Stride, and Chunk Views .................................................................................................. 633
Range Factories ........................................................................................................................ 642
Summary .................................................................................................................................. 647
Chapter 16: Time Library ............................................................................................................. 649
Ratios ........................................................................................................................................ 649
Durations ................................................................................................................................... 656
Clocks and Timepoints .............................................................................................................. 665
More Clocks and Timepoints ..................................................................................................... 675
Software Benchmarking ........................................................................................................... 680
Summary .................................................................................................................................. 687
Chapter 17: File Systems .......................................................................................................... 689
File System Path Classes .......................................................................................................... 689
File System Directory Iterator Classes ...................................................................................... 699
File System Helper Functions ................................................................................................... 709
File System Copy Functions ...................................................................................................... 718
Summary .................................................................................................................................. 725
Chapter 18: Numerical Processing – Part 1 ................................................................................. 727
Mathematical Constants ........................................................................................................... 727
Complex Numbers ..................................................................................................................... 734
Random Number Generation ..................................................................................................... 743
Using Generators and Distributions ..................................................................................... 747
Dice Games ......................................................................................................................... 755
Vector of Random Numbers ................................................................................................. 764
Summary .................................................................................................................................. 767
Chapter 19: Numerical Processing – Part 2 ...................................................................................... 769
Class std::valarray ..................................................................................................................... 769
Arithmetic Functions ........................................................................................................... 770
Statistical Calculations ........................................................................................................ 777
Class std::slice .......................................................................................................................... 785
Basic Operations ................................................................................................................. 785
Covariance Matrix ................................................................................................................ 794
Inner Products and Reductions ................................................................................................. 799
Summary .................................................................................................................................. 808
Chapter 20: Concurrency – Part 1 ................................................................................................ 811
Concurrency Primer .................................................................................................................. 811
Execution Policies ..................................................................................................................... 813
Mutexes .................................................................................................................................... 821
Threads ..................................................................................................................................... 825
Atomic Operations ..................................................................................................................... 833
Multithreaded Algorithms .......................................................................................................... 841
Summary .................................................................................................................................. 851
Chapter 21: Concurrency – Part 2 ................................................................................................ 853
Semaphores .............................................................................................................................. 853
Latches ..................................................................................................................................... 861
Condition Variables ..................................................................................................................... 866
Futures ...................................................................................................................................... 873
Summary .................................................................................................................................. 886
Appendix A: Source Code and Development Tools .......................................................................... 887
Appendix B: References and Resources ........................................................................................ 895
Index ....................................................................................................................................... 899
Learn how to use the classes, algorithms, and other programming constructs of C++ STL. This comprehensive and practical guide covers a broad range of STL programming topics and highlights numerous programming constructs from the C++20 and C++23 standards.
Mastering use of STL can be daunting for both new and experienced C++ programmers. It doesn’t help that the ISO C++ specification documents are meticulous and verbose. This book is organized to help you quickly understand C++ STL programming, focusing on the real-world aspects of its usage. Rather than spend time providing extensive explanations of the inner workings of STL, author Daniel Kusswurm judiciously explores these intricacies only when they advance the reader’s understanding of a particular topic.
This book is accompanied by over 100 source code examples, designed to accelerate learning by emphasizing practical use cases. It coincides with the C++20 and C++23 standards and works with any OS platform that supports these standards including Windows (Visual C++), Linux (GNU C++) and macOS (clang). After reading Practical C++ STL Programming, you’ll be able to exploit the computational capabilities of STL to implement a wide variety of software algorithms and solve unique programming challenges.
Software developers and programmers who understand the basic syntax and semantics of C++ and want/need to learn how to use STL’s classes and algorithms, or programmers who have experience using STL as specified by the C++11/14/17 standards and are interested in learning how to use the new STL classes and algorithms of C++20/23. Computer science/engineering students or hobbyists who want to learn about or better understand the capabilities of STL.