Book Image

Mastering the C++17 STL

By : Arthur O'Dwyer
Book Image

Mastering the C++17 STL

By: Arthur O'Dwyer

Overview of this book

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.
Table of Contents (13 chapters)

The useful adaptor: std::priority_queue<T>

In Chapter 3, The Iterator-Pair Algorithms, we introduced the family of "heap" algorithms: make_heap, push_heap, and pop_heap. You can use these algorithms to give a range of elements the max-heap property. If you maintain the max-heap property on your data as an invariant, you get a data structure commonly known as a priority queue. In data-structure textbooks, a priority queue is often depicted as a kind of binary tree, but as we saw in Chapter 3, The Iterator-Pair Algorithms, there's nothing about the max-heap property that requires an explicitly pointer-based tree structure.

The standard container std::priority_queue<T, Ctr, Cmp> represents a priority queue, represented internally as an instance of Ctr where the elements of the Ctr are invariably in max-heap order (as determined by an instance of the comparator...