C++17 STL Cookbook

Book description

Over 90 recipes that leverage the powerful features of the Standard Library in C++17

About This Book

  • Learn the latest features of C++ and how to write better code by using the Standard Library (STL). Reduce the development time for your applications.

  • Understand the scope and power of STL features to deal with real-world problems.

  • Compose your own algorithms without forfeiting the simplicity and elegance of the STL way.

  • 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 the newest version of C++: C++ 17.

    What You Will Learn

  • Learn about the new core language features and the problems they were intended to solve

  • Understand the inner workings and requirements of iterators by implementing them

  • Explore algorithms, functional programming style, and lambda expressions

  • Leverage the rich, portable, fast, and well-tested set of well-designed algorithms provided in the STL

  • Work with strings the STL way instead of handcrafting C-style code

  • Understand standard support classes for concurrency and synchronization, and how to put them to work

  • Use the filesystem library addition available with the C++17 STL

  • In Detail

    C++ has come a long way and is in use in every area of the industry. Fast, efficient, and flexible, it is used to solve many problems. The upcoming version of C++ will see programmers change the way they code. If you want to grasp the practical usefulness of the C++17 STL in order to write smarter, fully portable code, then this book is for you.

    Beginning with new language features, this book will help you understand the language’s mechanics and library features, and offers insight into how they work. Unlike other books, ours takes an implementation-specific, problem-solution approach that will help you quickly overcome hurdles. You will learn the core STL concepts, such as containers, algorithms, utility classes, lambda expressions, iterators, and more, while working on practical real-world recipes. These recipes will help you get the most from the STL and show you how to program in a better way.

    By the end of the book, you will be up to date with the latest C++17 features and save time and effort while solving tasks elegantly using the STL.

    Style and approach

    This recipe-based guide will show you how to make the best use of C++ together with the STL to squeeze more out of the standard language

    Table of contents

    1. Preface
      1. What this book covers
      2. What you need for this book
        1. Compiling and running the recipes
        2. Requirements for early adopters
      3. Who this book is for
      4. Sections
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      5. Conventions
      6. Reader feedback
      7. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    2. The New C++17 Features
      1. Introduction
      2. Using structured bindings to unpack bundled return values
        1. How to do it...
        2. How it works...
        3. There's more...
      3. Limiting variable scopes to if and switch statements
        1. How to do it...
        2. How it works...
        3. There's more...
      4. Profiting from the new bracket initializer rules
        1. How to do it...
        2. How it works...
      5. Letting the constructor automatically deduce the resulting template class type
        1. How to do it...
        2. How it works...
        3. There's more...
      6. Simplifying compile time decisions with constexpr-if
        1. How to do it...
        2. How it works...
        3. There's more...
      7. Enabling header-only libraries with inline variables
        1. How it's done...
        2. How it works...
        3. There's more...
      8. Implementing handy helper functions with fold expressions
        1. How to do it...
        2. How it works...
        3. There's more...
          1. Match ranges against individual items
          2. Check if multiple insertions into a set are successful
          3. Check if all the parameters are within a certain range
          4. Pushing multiple items into a vector
    3. STL Containers
      1. Introduction
        1. Contiguous storage
        2. List storage
        3. Search trees
        4. Hash tables
        5. Container adapters
      2. Using the erase-remove idiom on std::vector
        1. How to do it...
        2. How it works...
        3. There's more...
      3. Deleting items from an unsorted std::vector in O(1) time
        1. How to do it...
        2. How it works...
      4. Accessing std::vector instances the fast or the safe way
        1. How to do it...
        2. How it works...
        3. There's more...
      5. Keeping std::vector instances sorted
        1. How to do it...
        2. How it works...
        3. There's more...
      6. Inserting items efficiently and conditionally into std::map
        1. How to do it...
        2. How it works...
        3. There's more...
      7. Knowing the new insertion hint semantics of std::map::insert
        1. How to do it...
        2. How it works...
        3. There's more...
      8. Efficiently modifying the keys of std::map items
        1. How to do it...
        2. How it works...
        3. There's more...
      9. Using std::unordered_map with custom types
        1. How to do it...
        2. How it works...
      10. Filtering duplicates from user input and printing them in alphabetical order with std::set
        1. How to do it...
        2. How it works...
          1. std::istream_iterator
          2. std::inserter
          3. Putting it together
      11. Implementing a simple RPN calculator with std::stack
        1. How to do it...
        2. How it works...
          1. Stack handling
          2. Distinguishing operands from operations from user input
          3. Selecting and applying the right mathematical operation
        3. There's more...
      12. Implementing a word frequency counter with std::map
        1. How to do it...
        2. How it works...
      13. Implement a writing style helper tool for finding very long sentences in text with std::multimap
        1. How to do it...
        2. How it works...
        3.  There's more...
      14. Implementing a personal to-do list using std::priority_queue
        1. How to do it...
        2. How it works...
    4. Iterators
      1. Introduction
        1. Iterator categories
          1. Input iterator
          2. Forward iterator
          3. Bidirectional iterator
          4. Random access iterator
          5. Contiguous iterator
          6. Output iterator
          7. Mutable iterator
      2. Building your own iterable range
        1. How to do it...
        2. How it works...
      3. Making your own iterators compatible with STL iterator categories
        1. How to do it...
        2. How it works...
        3. There's more...
      4. Using iterator adapters to fill generic data structures
        1. How to do it...
        2. How it works...
          1. std::back_insert_iterator
          2. std::front_insert_iterator
          3. std::insert_iterator
          4. std::istream_iterator
          5. std::ostream_iterator
      5. Implementing algorithms in terms of iterators
        1. How to do it...
        2. There's more...
      6. Iterating the other way around using reverse iterator adapters
        1. How to do it...
        2. How it works...
      7. Terminating iterations over ranges with iterator sentinels
        1. How to do it...
      8. Automatically checking iterator code with checked iterators
        1. How to do it...
        2. How it works...
        3. There's more...
      9. Building your own zip iterator adapter
        1. How to do it...
        2. There's more...
          1. Ranges library
    5. Lambda Expressions
      1. Introduction
      2. Defining functions on the run using lambda expressions
        1. How to do it...
        2. How it works...
          1. Capture list
          2. mutable (optional)
          3. constexpr (optional)
          4. exception attr (optional)
          5. return type (optional)
      3. Adding polymorphy by wrapping lambdas into std::function
        1. How to do it...
        2. How it works...
      4. Composing functions by concatenation
        1. How to do it...
        2. How it works...
      5. Creating complex predicates with logical conjunction
        1. How to do it...
        2. There's more...
      6. Calling multiple functions with the same input
        1. How to do it...
        2. How it works...
      7. Implementing transform_if using std::accumulate and lambdas
        1. How to do it...
        2. How it works...
      8. Generating cartesian product pairs of any input at compile time
        1. How to do it...
        2. How it works...
    6. STL Algorithm Basics
      1. Introduction
      2. Copying items from containers to other containers
        1. How to do it...
        2. How it works...
      3. Sorting containers
        1. How to do it...
        2. How it works...
      4. Removing specific items from containers
        1. How to do it...
        2. How it works...
      5. Transforming the contents of containers
        1. How to do it...
        2. How it works...
      6. Finding items in ordered and unordered vectors
        1. How to do it...
        2. How it works...
      7. Limiting the values of a vector to a specific numeric range with std::clamp
        1. How to do it...
        2. How it works...
      8. Locating patterns in strings with std::search and choosing the optimal implementation
        1. How to do it...
        2. How it works...
      9. Sampling large vectors
        1. How to do it...
        2. How it works...
      10. Generating permutations of input sequences
        1. How to do it...
        2. How it works...
      11. Implementing a dictionary merging tool
        1. How to do it...
        2. How it works...
    7. Advanced Use of STL Algorithms
      1. Introduction
      2. Implementing a trie class using STL algorithms
        1. How to do it...
        2. How it works...
      3. Implementing a search input suggestion generator with tries
        1. How to do it...
        2. How it works...
        3. There's more...
      4. Implementing the Fourier transform formula with STL numeric algorithms
        1. How to do it...
        2. How it works...
      5. Calculating the error sum of two vectors
        1. How to do it...
        2. How it works...
      6. Implementing an ASCII Mandelbrot renderer
        1. How to do it...
        2. How it works...
      7. Building our own algorithm - split
        1. How to do it...
        2. How it works...
        3. There's more...
      8. Composing useful algorithms from standard algorithms - gather
        1. How to do it...
        2. How it works...
      9. Removing consecutive whitespace between words
        1. How to do it...
        2. How it works...
      10. Compressing and decompressing strings
        1. How to do it...
        2. How it works...
        3. There's more...
    8. Strings, Stream Classes, and Regular Expressions
      1. Introduction
      2. Creating, concatenating, and transforming strings
        1. How to do it...
        2. How it works...
      3. Trimming whitespace from the beginning and end of strings
        1. How to do it...
        2. How it works...
      4. Getting the comfort of std::string without the cost of constructing std::string objects
        1. How to do it...
        2. How it works...
      5. Reading values from user input
        1. How to do it...
        2. How it works...
      6. Counting all words in a file
        1. How to do it...
        2. How it works...
      7. Formatting your output with I/O stream manipulators
        1. How to do it...
        2. How it works...
      8. Initializing complex objects from file input
        1. How to do it...
        2. How it works...
      9. Filling containers from std::istream iterators
        1. How to do it...
        2. How it works...
      10. Generic printing with std::ostream iterators
        1. How to do it...
        2. How it works...
      11. Redirecting output to files for specific code sections
        1. How to do it...
        2. How it works...
      12. Creating custom string classes by inheriting from std::char_traits
        1. How to do it...
        2. How it works...
      13. Tokenizing input with the regular expression library
        1. How to do it...
        2. How it works...
      14. Comfortably pretty printing numbers differently per context on the fly
        1. How to do it...
      15. Catching readable exceptions from std::iostream errors
        1. How to do it...
        2. How it works...
    9. Utility Classes
      1. Introduction
      2. Converting between different time units using std::ratio
        1. How to do it...
        2. How it works...
        3. There's more...
      3. Converting between absolute and relative times with std::chrono
        1. How to do it...
        2. How it works...
      4. Safely signalizing failure with std::optional
        1. How to do it...
        2. How it works...
      5. Applying functions on tuples
        1. How to do it...
        2. How it works...
      6. Quickly composing data structures with std::tuple
        1. How to do it...
        2. How it works...
          1. operator<< for tuples
          2. The zip function for tuples
      7. Replacing void* with std::any for more type safety
        1. How to do it...
        2. How it works...
      8. Storing different types with std::variant
        1. How to do it...
        2. How it works...
      9. Automatically handling resources with std::unique_ptr
        1. How to do it...
        2. How it works...
      10. Automatically handling shared heap memory with std::shared_ptr
        1. How to do it...
        2. How it works...
        3. There's more...
      11. Dealing with weak pointers to shared objects
        1. How to do it...
        2. How it works...
      12. Simplifying resource handling of legacy APIs with smart pointers
        1. How to do it...
        2. How it works...
      13. Sharing different member values of the same object
        1. How to do it...
        2. How it works...
      14. Generating random numbers and choosing the right random number engine
        1. How to do it...
        2. How it works...
      15. Generating random numbers and letting the STL shape specific distributions
        1. How to do it...
        2. How it works...
    10. Parallelism and Concurrency
      1. Introduction
      2. Automatically parallelizing code that uses standard algorithms
        1. How to do it...
        2. How it works...
          1. Which STL algorithms can we parallelize this way?
          2. How do those execution policies work?
          3. What does vectorization mean?
      3. Putting a program to sleep for specific amounts of time
        1. How to do it...
        2. How it works...
      4. Starting and stopping threads
        1. How to do it...
        2. How it works...
      5. Performing exception safe shared locking with std::unique_lock and std::shared_lock
        1. How to do it...
        2. How it works...
          1. Mutex classes
          2. Lock classes
      6. Avoiding deadlocks with std::scoped_lock
        1. How to do it...
        2. How it works...
      7. Synchronizing concurrent std::cout use
        1. How to do it...
        2. How it works...
      8. Safely postponing initialization with std::call_once
        1. How to do it...
        2. How it works...
      9. Pushing the execution of tasks into the background using std::async
        1. How to do it...
        2. How it works...
        3. There's more...
      10. Implementing the producer/consumer idiom with std::condition_variable
        1. How to do it...
        2. How it works...
      11. Implementing the multiple producers/consumers idiom with std::condition_variable
        1. How to do it...
        2. How it works...
      12. Parallelizing the ASCII Mandelbrot renderer using std::async
        1. How to do it...
        2. How it works...
      13. Implementing a tiny automatic parallelization library with std::future
        1. How to do it...
        2. How it works...
    11. Filesystem
      1. Introduction
      2. Implementing a path normalizer
        1. How to do it...
        2. How it works...
        3. There's more...
      3. Getting canonical file paths from relative paths
        1. How to do it...
        2. How it works...
      4. Listing all files in directories
        1. How to do it...
        2. How it works...
      5. Implementing a grep-like text search tool
        1. How to do it...
        2. How it works...
        3. There's more...
      6. Implementing an automatic file renamer
        1. How to do it...
      7. Implementing a disk usage counter
        1. How to do it...
        2. How it works...
      8. Calculating statistics about file types
        1. How to do it...
      9. Implementing a tool that reduces folder size by substituting duplicates with symlinks
        1. How to do it...
        2. How it works...
        3. There's more...

    Product information

    • Title: C++17 STL Cookbook
    • Author(s): Jacek Galowicz
    • Release date: June 2017
    • Publisher(s): Packt Publishing
    • ISBN: 9781787120495