C++20 STL Cookbook

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

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

Product information

  • Title: C++20 STL Cookbook
  • Author(s): Bill Weinman
  • Release date: May 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781803248714