Mastering the C++17 STL

Book description

This book breaks down the C++ STL, teaching you how to extract its gems and apply them to your programming.

About This Book

  • Boost your productivity as a C++ developer with the latest features of C++17
  • Develop high-quality, fast, and portable applications with the varied features of the STL
  • Migrate from older versions (C++11, C++14) to C++17

Who This Book Is For

This book is for developers who would like to master the C++ STL and make full use of its components. Prior C++ knowledge is assumed.

What You Will Learn

  • Make your own iterator types, allocators, and thread pools.
  • Master every standard container and every standard algorithm.
  • Improve your code by replacing new/delete with smart pointers.
  • Understand the difference between monomorphic algorithms, polymorphic algorithms, and generic algorithms.
  • Learn the meaning and applications of vocabulary type, product type and sum type.

In Detail

Modern C++ has come a long way since 2011. The latest update, C++17, has just been ratified and several implementations are on the way.

This book is your guide to the C++ standard library, including the very latest C++17 features.

The book starts by exploring the C++ Standard Template Library in depth. You will learn the key differences between classical polymorphism and generic programming, the foundation of the STL. You will also learn how to use the various algorithms and containers in the STL to suit your programming needs. The next module delves into the tools of modern C++. Here you will learn about algebraic types such as std::optional, vocabulary types such as std::function, smart pointers, and synchronization primitives such as std::atomic and std::mutex. In the final module, you will learn about C++'s support for regular expressions and file I/O.

By the end of the book you will be proficient in using the C++17 standard library to implement real programs, and you'll have gained a solid understanding of the library's own internals.

Style and approach

This book takes a concise but comprehensive approach to explaining and applying the C++ STL, one feature at a time.

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Classical Polymorphism and Generic Programming
    1. Concrete monomorphic functions
    2. Classically polymorphic functions
    3. Generic programming with templates
    4. Summary
  3. Iterators and Ranges
    1. The problem with integer indices
    2. On beyond pointers
    3. Const iterators
    4. A pair of iterators defines a range
    5. Iterator categories
    6. Input and output iterators
    7. Putting it all together
    8. The deprecated std::iterator
    9. Summary
  4. The Iterator-Pair Algorithms
    1. A note about headers
    2. Read-only range algorithms
    3. Shunting data with std::copy
    4. Variations on a theme - std::move and std::move_iterator
    5. Complicated copying with std::transform
    6. Write-only range algorithms
    7. Algorithms that affect object lifetime
    8. Our first permutative algorithm: std::sort
    9. Swapping, reversing, and partitioning
    10. Rotation and permutation
    11. Heaps and heapsort
    12. Merges and mergesort
    13. Searching and inserting in a sorted array with std::lower_bound
    14. Deleting from a sorted array with std::remove_if
    15. Summary
  5. The Container Zoo
    1. The notion of ownership
    2. The simplest container: std::array<T, N>
    3. The workhorse: std::vector<T>
      1. Resizing a std::vector
      2. Inserting and erasing in a std::vector
      3. Pitfalls with vector<bool>
      4. Pitfalls with non-noexcept move constructors
    4. The speedy hybrid: std::deque<T>
    5. A particular set of skills: std::list<T>
      1. What are the special skills of std::list?
    6. Roughing it with std::forward_list<T>
    7. Abstracting with std::stack<T> and std::queue<T>
    8. The useful adaptor: std::priority_queue<T>
    9. The trees: std::set<T> and std::map<K, V>
      1. A note about transparent comparators
    10. Oddballs: std::multiset<T> and std::multimap<K, V>
      1. Moving elements without moving them
    11. The hashes: std::unordered_set<T> and std::unordered_map<K, V>
      1. Load factor and bucket lists
    12. Where does the memory come from?
    13. Summary
  6. Vocabulary Types
    1. The story of std::string
    2. Tagging reference types with reference_wrapper
    3. C++11 and algebraic types
    4. Working with std::tuple
      1. Manipulating tuple values
      2. A note about named classes
    5. Expressing alternatives with std::variant
      1. Visiting variants
      2. What about make_variant? and a note on value semantics
    6. Delaying initialization with std::optional
    7. Revisiting variant
    8. Infinite alternatives with std::any
      1. std::any versus polymorphic class types
    9. Type erasure in a nutshell
      1. std::any and copyability
    10. Again with the type erasure: std::function
      1. std::function, copyability, and allocation
    11. Summary
  7. Smart Pointers
    1. The origins of smart pointers
    2. Smart pointers never forget
    3. Automatically managing memory with std::unique_ptr<T>
      1. Why C++ doesn't have the finally keyword
      2. Customizing the deletion callback
      3. Managing arrays with std::unique_ptr<T[]>
    4. Reference counting with std::shared_ptr<T>
      1. Don't double-manage!
      2. Holding nullable handles with weak_ptr
      3. Talking about oneself with std::enable_shared_from_this
      4. The Curiously Recurring Template Pattern
      5. A final warning
    5. Denoting un-special-ness with observer_ptr<T>
    6. Summary
  8. Concurrency
    1. The problem with volatile
    2. Using std::atomic<T> for thread-safe accesses
      1. Doing complicated operations atomically
      2. Big atomics
    3. Taking turns with std::mutex
      1. "Taking locks" the right way
    4. Always associate a mutex with its controlled data
    5. Special-purpose mutex types
      1. Upgrading a read-write lock
      2. Downgrading a read-write lock
    6. Waiting for a condition
    7. Promises about futures
    8. Packaging up tasks for later
    9. The future of futures
    10. Speaking of threads...
      1. Identifying individual threads and the current thread
    11. Thread exhaustion and std::async
    12. Building your own thread pool
      1. Improving our thread pool's performance
    13. Summary
  9. Allocators
    1. An allocator is a handle to a memory resource
      1. Refresher - Interfaces versus concepts
    2. Defining a heap with memory_resource
    3. Using the standard memory resources
      1. Allocating from a pool resource
    4. The 500 hats of the standard allocator
      1. Carrying metadata with fancy pointers
    5. Sticking a container to a single memory resource
    6. Using the standard allocator types
      1. Setting the default memory resource
    7. Making a container allocator-aware
    8. Propagating downwards with scoped_allocator_adaptor
      1. Propagating different allocators
    9. Summary
  10. Iostreams
    1. The trouble with I/O in C++
    2. Buffering versus formatting
    3. Using the POSIX API
    4. Using the standard C API
      1. Buffering in the standard C API
      2. Formatting with printf and snprintf
    5. The classical iostreams hierarchy
      1. Streaming and manipulators
      2. Streaming and wrappers
      3. Solving the sticky-manipulator problem
      4. Formatting with ostringstream
      5. A note on locales
    6. Converting numbers to strings
    7. Converting strings to numbers
    8. Reading a line or word at a time
    9. Summary
  11. Regular Expressions
    1. What are regular expressions?
      1. A note on backslash-escaping
    2. Reifying regular expressions into std::regex objects
    3. Matching and searching
      1. Pulling submatches out of a match
      2. Converting submatches to data values
    4. Iterating over multiple matches
    5. Using regular expressions for string replacement
    6. A primer on the ECMAScript regex grammar
      1. Non-consuming constructs
      2. Obscure ECMAScript features and pitfalls
    7. Summary
  12. Random Numbers
    1. Random numbers versus pseudo-random numbers
    2. The problem with rand()
    3. Solving problems with <random>
    4. Dealing with generators
      1. Truly random bits with std::random_device
      2. Pseudo-random bits with std::mt19937
      3. Filtering generator outputs with adaptors
    5. Dealing with distributions
      1. Rolling dice with uniform_int_distribution
      2. Generating populations with normal_distribution
      3. Making weighted choices with discrete_distribution
      4. Shuffling cards with std::shuffle
    6. Summary
  13. Filesystem
    1. A note about namespaces
    2. A very long note on error-reporting
      1. Using <system_error>
      2. Error codes and error conditions
      3. Throwing errors with std::system_error
    3. Filesystems and paths
      1. Representing paths in C++
      2. Operations on paths
    4. Statting files with directory_entry
    5. Walking directories with directory_iterator
      1. Recursive directory walking
    6. Modifying the filesystem
    7. Reporting disk usage
    8. Summary

Product information

  • Title: Mastering the C++17 STL
  • Author(s): Arthur O'Dwyer
  • Release date: September 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781787126824