Book description
Proven techniques to improve the efficiency of your programs using the Standard Template Library (STL)
Key Features
- Become familiar with the latest features of C++20 and write better code using the STL
- Reduce the development time for your applications and enable faster deployment
- Get up and running with the new and leaner STL capabilities introduced in the latest release
Book Description
Fast, efficient, and flexible, the C++ programming language has come a long way and is used in every area of the industry to solve many problems. The latest version C++20 will see programmers change the way they code as it brings a whole array of features enabling the quick deployment of applications. This book will get you up and running with using the STL in the best way possible.
Beginning with new language features in C++20, this book will help you understand the language's mechanics and library features and offer insights into how they work. Unlike other books, the C++20 STL Cookbook takes an implementation-specific, problem-solution approach that will help you overcome hurdles quickly. You'll learn core STL concepts, such as containers, algorithms, utility classes, lambda expressions, iterators, and more, while working on real-world recipes. This book is a reference guide for using the C++ STL with its latest capabilities and exploring the cutting-edge features in functional programming and lambda expressions.
By the end of the book C++20 book, you'll be able to leverage the latest C++ features and save time and effort while solving tasks elegantly using the STL.
What you will learn
- Understand the new language features and the problems they can solve
- Implement generic features of the STL with practical examples
- Understand standard support classes for concurrency and synchronization
- Perform efficient memory management using the STL
- Implement seamless formatting using std::format
- Work with strings the STL way instead of handcrafting C-style code
Who this book is for
This book is for intermediate to advanced C++ programmers who want to get the most out of the Standard Template Library of C++20, the newest version of C++. Basic knowledge of coding and C++ concepts is necessary to get started with this book.
Table of contents
- C++20 STL Cookbook
- Contributors
- About the author
- About the reviewer
- Preface
-
Chaper 1: New C++20 Features
- Technical requirements
- Format text with the new format library
- Use compile-time vectors and strings with constexpr
- Safely compare integers of different types
- Use the "spaceship" operator <=> for three-way comparisons
- Easily find feature test macros with the <version> header
- Create safer templates with concepts and constraints
- Avoid re-compiling template libraries with modules
- Create views into containers with ranges
- Chapter 2: General STL Features
-
Chapter 3: STL Containers
- A quick overview of the STL container types
- Technical requirements
- Use uniform erasure functions to delete items from a container
- Delete items from an unsorted vector in constant time
- Access vector elements directly and safely
- Keep vector elements sorted
- Efficiently insert elements into a map
- Efficiently modify the keys of map items
- Use unordered_map with custom keys
- Use set to sort and filter user input
- A simple RPN calculator with deque
- A word frequency counter with map
- Find long sentences with a vector of vectors
- A ToDo list using multimap
-
Chapter 4: Compatible Iterators
- Iterators are fundamental
- Technical requirements
- Create an iterable range
- Make your iterators compatible with STL iterator traits
- Use iterator adapters to fill STL containers
- Create a generator as iterators
- Use reverse iterator adapters to iterate backward
- Iterate objects of unknown length with a sentinel
- Build a zip iterator adapter
- Create a random-access iterator
-
Chapter 5: Lambda Expressions
- Lambda expressions
- Technical requirements
- Use lambdas for scoped reusable code
- Use lambdas as predicates with the algorithm library
- Use std::function as a polymorphic wrapper
- Concatenate lambdas with recursion
- Combine predicates with logical conjunction
- Call multiple lambdas with the same input
- Use mapped lambdas for a jump table
-
Chapter 6: STL Algorithms
- Technical requirements
- Copy from one iterator to another
- Join container elements into a string
- Sort containers with std::sort
- Modify containers with std::transform
- Find items in a container
- Limit the values of a container to a range with std::clamp
- Sample data sets with std::sample
- Generate permutations of data sequences
- Merge sorted containers
-
Chapter 7: Strings, Streams, and Formatting
- String formatting
- Technical requirements
- Use string_view as a lightweight string object
- Concatenate strings
- Transform strings
- Format text with C++20's format library
- Trim whitespace from strings
- Read strings from user input
- Count words in a file
- Initialize complex structures from file input
- Customize a string class with char_traits
- Parse strings with Regular Expressions
-
Chapter 8: Utility Classes
- Technical requirements
- Manage optional values with std::optional
- Use std::any for type safety
- Store different types with std::variant
- Time events with std::chrono
- Use fold expressions for variadic tuples
- Manage allocated memory with std::unique_ptr
- Share objects with std::shared_ptr
- Use weak pointers with shared objects
- Share members of a managed object
- Compare random number engines
- Compare random number distribution generators
-
Chapter 9: Concurrency and Parallelism
- Technical requirements
- Sleep for a specific amount of time
- Use std::thread for concurrency
- Use std::async for concurrency
- Run STL algorithms in parallel with execution policies
- Share data safely with mutex and locks
- Share flags and values with std::atomic
- Initialize threads with std::call_once
- Use std::condition_variable to resolve the producer-consumer problem
- Implement multiple producers and consumers
- Chapter 10: Using the File System
- Chapter 11: A Few More Ideas
- Other Books You May Enjoy
Product information
- Title: C++20 STL Cookbook
- Author(s):
- Release date: May 2022
- Publisher(s): Packt Publishing
- ISBN: 9781803248714
You might also like
book
C++17 STL Cookbook
Over 90 recipes that leverage the powerful features of the Standard Library in C++17 About This …
book
C++ Cookbook
Despite its highly adaptable and flexible nature, C++ is also one of the more complex programming …
book
Embedded Programming with Modern C++ Cookbook
Explore various constraints and challenges that embedded developers encounter in their daily tasks and learn how …
book
Mastering the C++17 STL
This book breaks down the C++ STL, teaching you how to extract its gems and apply …