C++20 STL Cookbook: Leverage the latest features of the STL to solve real-world problems

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

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

  1. New C++20 Features
  2. General STL Features
  3. STL Containers
  4. Compatible Iterators
  5. Lambda Expressions
  6. STL Algorithms
  7. Strings, Streams, and Formatting
  8. Utility Classes
  9. Concurrency and Parallelism
  10. Using the File System
  11. A Few More Ideas

Author(s): Bill Weinman
Edition: 1
Publisher: Packt Publishing
Year: 2022

Language: English
Pages: 450
Tags: C++; C++20; STL; C++ STL; Containers; Iterators; Lambda Expressions; STL Algorithms; Strings; Streams; Concurrency; Parallelism; Standard Template Library; C++ Programming

Cover
Title Page
Copyright and Credits
Contributors
Table of Contents
Preface
Chapter 1: New C++20 Features
Technical requirements
Format text with the new format library
How to do it…
How it works…
There's more…
Use compile-time vectors and strings with constexpr
How to do it…
How it works…
Safely compare integers of different types
How to do it…
How it works…
Use the "spaceship" operator <=> for three-way comparisons
How to do it…
How it works…
There's more…
Easily find feature test macros with the header
How to do it…
How it works…
Create safer templates with concepts and constraints
How to do it…
How it works…
There's more…
Avoid re-compiling template libraries with modules
How to do it…
How it works…
Create views into containers with ranges
How to do it…
How it works…
There's more…
Chapter 2: General STL Features
Technical requirements
Use the new span class to make your C-arrays safer
How to do it…
How it works…
Use structured binding to return multiple values
How to do it…
How it works…
Initialize variables within if and switch statements
How to do it…
How it works…
There's more…
Use template argument deduction for simplicity and clarity
How to do it…
How it works…
There's more…
Use if constexpr to simplify compile-time decisions
How to do it…
How it works…
Chapter 3: STL Containers
A quick overview of the STL container types
Sequential containers
Associative containers
Container adapters
Technical requirements
Use uniform erasure functions to delete items from a container
How to do it…
How it works…
Delete items from an unsorted vector in constant time
How to do it…
How it works…
Access vector elements directly and safely
How to do it…
How it works…
There's more…
Keep vector elements sorted
How to do it…
How it works…
There's more…
Efficiently insert elements into a map
How to do it…
How it works…
Efficiently modify the keys of map items
How to do it…
How it works…
There's more…
Use unordered_map with custom keys
How to do it…
How it works…
Use set to sort and filter user input
How to do it…
How it works…
A simple RPN calculator with deque
How to do it…
How it works…
There's more…
A word frequency counter with map
How to do it…
How it works…
Find long sentences with a vector of vectors
How to do it…
How it works…
A ToDo list using multimap
How to do it…
How it works…
Chapter 4: Compatible Iterators
Iterators are fundamental
Iterator categories
Iterator concepts
Technical requirements
Create an iterable range
How to do it…
How it works…
There's more…
Make your iterators compatible with STL iterator traits
How to do it…
How it works…
There's more…
Use iterator adapters to fill STL containers
How to do it…
How it works…
Create a generator as iterators
How to do it…
How it works…
There's more…
Use reverse iterator adapters to iterate backward
How to do it…
How it works…
Iterate objects of unknown length with a sentinel
How to do it…
How it works…
Build a zip iterator adapter
How to do it…
How it works…
There's more…
Create a random-access iterator
How to do it…
How it works…
Chapter 5: Lambda Expressions
Lambda expressions
Closures
Technical requirements
Use lambdas for scoped reusable code
How to do it…
How it works…
Use lambdas as predicates with the algorithm library
How to do it…
How it works…
Use std::function as a polymorphic wrapper
How to do it…
How it works…
There's more…
Concatenate lambdas with recursion
How to do it…
How it works…
Combine predicates with logical conjunction
How to do it…
How it works…
Call multiple lambdas with the same input
How to do it…
How it works…
Use mapped lambdas for a jump table
How to do it…
How it works…
Chapter 6: STL Algorithms
Technical requirements
Copy from one iterator to another
How to do it…
How it works…
Join container elements into a string
How to do it…
How it works…
There's more…
Sort containers with std::sort
How to do it…
How it works…
Modify containers with std::transform
How to do it…
How it works…
Find items in a container
How to do it…
How it works…
There's more…
Limit the values of a container to a range with std::clamp
How to do it…
How it works…
Sample data sets with std::sample
How to do it…
How it works…
Generate permutations of data sequences
How to do it…
How it works…
Merge sorted containers
How to do it…
How it works…
Chapter 7: Strings, Streams, and Formatting
String formatting
Technical requirements
Use string_view as a lightweight string object
How to do it…
How it works…
Concatenate strings
How to do it…
How it works…
There's more…
Why would I choose one over another?
Transform strings
How to do it…
How it works…
Format text with C++20's format library
How to do it…
How it works…
There's more…
Trim whitespace from strings
How to do it…
How it works…
Read strings from user input
How to do it…
How it works…
Count words in a file
How to do it…
How it works…
Initialize complex structures from file input
How to do it…
How it works…
There's more…
Customize a string class with char_traits
How to do it…
How it works…
There's more…
Parse strings with Regular Expressions
How to do it…
How it works…
Chapter 8: Utility Classes
Technical requirements
Manage optional values with std::optional
How to do it…
How it works…
There's more…
Use std::any for type safety
How to do it…
How it works…
Store different types with std::variant
Differences from the primitive union structure
How to do it…
How it works…
Time events with std::chrono
How to do it…
How it works…
Use fold expressions for variadic tuples
Fold expressions
How to do it…
How it works…
There's more…
Manage allocated memory with std::unique_ptr
How to do it…
How it works…
Share objects with std::shared_ptr
How to do it…
How it works…
Use weak pointers with shared objects
How to do it…
How it works…
There's more…
Share members of a managed object
How to do it…
How it works…
Compare random number engines
How to do it…
How it works…
There's more…
Compare random number distribution generators
How to do it…
How it works…
Chapter 9: Concurrency and Parallelism
Technical requirements
Sleep for a specific amount of time
How to do it…
How it works…
There's more…
Use std::thread for concurrency
How to do it…
How it works…
There's more…
Use std::async for concurrency
How to do it…
How it works…
Run STL algorithms in parallel with execution policies
How to do it…
How it works…
Share data safely with mutex and locks
How to do it…
How it works…
There's more…
Share flags and values with std::atomic
How to do it…
How it works…
There's more…
Initialize threads with std::call_once
How to do it…
How it works…
Use std::condition_variable to resolve the producer-consumer problem
How to do it…
How it works…
Implement multiple producers and consumers
How to do it…
How it works…
Chapter 10: Using the File System
Technical requirements
Specialize std::formatter for the path class
How to do it…
How it works…
Use manipulation functions with path
How to do it…
How it works…
List files in a directory
How to do it…
How it works…
There's more…
Search directories and files with a grep utility
How to do it…
How it works…
See also…
Rename files with regex and directory_iterator
How to do it…
How it works…
See also…
Create a disk usage counter
How to do it…
How it works…
Chapter 11: A Few More Ideas
Technical requirement
Create a trie class for search suggestions
How to do it…
How it works…
Calculate the error sum of two vectors
How to do it…
How it works…
There's more…
Build your own algorithm: split
How to do it…
How it works…
Leverage existing algorithms: gather
How to do it…
How it works…
Remove consecutive whitespace
How to do it…
How it works…
Convert numbers to words
How to do it…
How it works…
There's more…
Index
Other Books You May Enjoy