Using the C++ Standard Template Libraries

Book description

Using the C++ Standard Template Libraries is a contemporary treatment that teaches the generic programming capabilities that the C++ 14 Standard Library provides. In this book, author Ivor Horton explains what the class and function templates available with C++ 14 do, and how to use them in a practical context.

You'll learn how to create containers, and how iterators are used with them to access, modify, and extend the data elements they contain. You'll also learn about stream iterators that can transfer data between containers and streams, including file streams. The function templates that define algorithms are explained in detail, and you'll learn how to pass function objects or lambda expressions to them to customize their behavior. Many working examples are included to demonstrate how to apply the algorithms with different types of containers.

After reading this book, you will understand the scope and power of the templates that the C++ 14 Standard Library includes and how these can greatly reduce the coding and development time for many applications. You'll be able to combine the class and function templates to great effect in dealing with real-world problems.

The templates in the Standard Library provide you as a C++ programmer with a comprehensive set of efficiently implemented generic programming tools that you can use for most types of application.

Table of contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. About the Author
  8. About the Techincal reviewer
  9. Acknowledgments
  10. Introduction
  11. Chapter 1: Introducing the Standard Template Library
    1. Basic Ideas
    2. Templates
    3. The Containers
    4. Iterators
      1. Obtaining Iterators
      2. Iterator Categories
      3. Stream Iterators
      4. Iterator Adaptors
    5. Operations on Iterators
    6. Smart Pointers
      1. Using unique_ptr<T> Pointers
      2. Using shared_ptr<T> Pointers
      3. weak_ptr<T> Pointers
    7. Algorithms
    8. Passing a Function as an Argument
      1. Function Objects
      2. Lambda Expressions
    9. Summary
  12. Chapter 2: Using Sequence Containers
    1. The Sequence Containers
      1. Function Members That Are Common Between Containers
    2. Using array<T,N> Containers
      1. Accessing Elements
      2. Using Iterators with array Containers
      3. Comparing array Containers
    3. Using vector<T> Containers
      1. Creating vector<T> Containers
      2. The Capacity and Size of a Vector
      3. Accessing Elements
      4. Using Iterators with a vector Container
      5. Adding New Elements to a vector Container
      6. Deleting Elements
      7. vector<bool> Containers
    4. Using deque<T> Containers
      1. Creating deque Containers
      2. Accessing Elements
      3. Adding and Removing Elements
      4. Replacing the Contents of a deque Container
    5. Using a list<T> Container
      1. Creating list Containers
      2. Adding Elements
      3. Removing Elements
      4. Sorting and Merging Elements
      5. Accessing Elements
    6. Using forward_list<T> Containers
    7. Defining Your Own Iterators
      1. STL Iterator Requirements
      2. The STL Approach
    8. Summary
  13. Chapter 3: Container Adapters
    1. What Are Container Adapters?
    2. Creating and Using a stack<T> Container Adapter
      1. Stack Operations
    3. Creating and Using a queue<T> Container Adapter
      1. Queue Operations
      2. A Practical Use of a Queue Container
    4. Using a priority_queue<T> Container Adapter
      1. Creating a Priority Queue
      2. Operations for a Priority Queue
    5. Heaps
      1. Creating a Heap
      2. Heap Operations
    6. Storing Pointers in a Container
      1. Storing Pointers in Sequence Containers
      2. Storing Pointers in a Priority Queue
      3. Heaps of Pointers
      4. Containers of Base Class Pointers
      5. Applying Algorithms to a Range of Pointers
    7. Summary
  14. Chapter 4: Map Containers
    1. Introducing Map Containers
    2. Using a map Container
      1. Creating a map Container
      2. Inserting Elements in a map
      3. Constructing map Elements in Place
      4. Accessing Elements in a map
      5. Deleting Elements
    3. Using pair<> and tuple<> Objects
      1. Operations with a pair
      2. Operations with a tuple
      3. tuples and pairs in Action
    4. Using a multimap Container
    5. Changing the Comparison Function
      1. Using a greater<T> Object
      2. Defining Your Own Function Object for Comparing Elements
    6. Hashing
      1. Functions That Generate Hash Values
    7. Using an unordered_map Container
      1. Creating and Managing unordered_map Containers
      2. Adjusting the Bucket Count
      3. Inserting Elements
      4. Accessing Elements
      5. Removing Elements
      6. Accessing Buckets
    8. Using an unordered_multimap Container
    9. Summary
  15. Chapter 5: Working with Sets
    1. Understanding Set Containers
    2. Using set<T> Containers
      1. Adding and Removing Elements
      2. Accessing Elements
      3. Working with sets
      4. Set Iterators
      5. Storing Pointers in a set Container
    3. Using multiset<T> Containers
      1. Storing Pointers to Derived Class Objects
    4. unordered_set<T> Containers
      1. Adding Elements
      2. Retrieving Elements
      3. Deleting Elements
      4. Producing a Bucket List
    5. Using unordered_multiset<T> Containers
    6. Operations on Sets
      1. The set_union() Algorithm
      2. The set_intersection() Algorithm
      3. The set_difference() Algorithm
      4. The set_symmetric_difference() Algorithm
      5. The includes() Algorithm
      6. Set Operations in Action
    7. Summary
  16. Chapter 6: Sorting, Merging, Searching, and Partitioning
    1. Sorting a Range
      1. Sorting and the Order of Equal Elements
      2. Partial Sorting
      3. Testing for Sorted Ranges
    2. Merging Ranges
    3. Searching a Range
      1. Finding any of a Range of Elements in a Range
      2. Finding Multiple Elements from a Range
    4. Partitioning a Range
      1. The partition_copy() Algorithm
      2. The partition_point() Algorithm
    5. Binary Search Algorithms
      1. The binary_search() Algorithm
      2. The lower_bound() Algorithm
      3. The equal_range() algorithm
    6. Summary
  17. Chapter 7: More Algorithms
    1. Testing Element Properties
    2. Comparing Ranges
      1. Finding Where Ranges Differ
      2. Lexicographical Range Comparisons
      3. Permutations of Ranges
    3. Copying a Range
      1. Copying a Number of Elements
      2. Conditional Copying
      3. Reverse Order Copying
    4. Copying and Reversing the Order of Elements
    5. Copying a Range Removing Adjacent Duplicates
    6. Removing Adjacent Duplicates from a Range
    7. Rotating Ranges
    8. Moving a Range
    9. Removing Elements from a Range
    10. Setting and Modifying Elements in a Range
      1. Generating Element Values with a Function
      2. Transforming a Range
      3. Replacing Elements in a Range
    11. Applying Algorithms
    12. Summary
  18. Chapter 8: Generating Random Numbers
    1. What Is a Random Number?
    2. Probability, Distributions, and Entropy
      1. What Is Probability?
      2. What Is a Distribution?
      3. What Is Entropy?
    3. Generating Random Numbers with the STL
      1. Seeds in Random Number Generation
      2. Obtaining Random Seeds
      3. Seed Sequences
    4. Distribution Classes
      1. The Default Random Number Generator
      2. Creating Distribution Objects
      3. Uniform Distributions
      4. Normal Distributions
      5. Lognormal Distributions
      6. Other Distributions Related to the Normal Distribution
      7. Sampling Distributions
      8. Other Distributions
    5. Random Number Engines and Generators
      1. The Linear Congruential Engine
      2. The Mersenne Twister Engine
      3. The Subtract with Carry Engine
    6. Shuffling a Range of Elements
    7. Summary
  19. Chapter 9: Stream Operations
    1. Stream Iterators
      1. Input Stream Iterators
      2. Output Stream Iterators
    2. Overloading the Insertion and Extraction Operators
    3. Using Stream Iterators with Files
      1. File Streams
      2. File Stream Class Templates
      3. File Input Using a Stream Iterator
      4. Repeated File Reads Using a Stream Iterator
      5. File Output Using a Stream Iterator
    4. Stream Iterators and Algorithms
    5. Stream Buffer Iterators
      1. Input Stream Buffer Iterators
      2. Output Stream Buffer Iterators
      3. Using Stream Buffer Iterators with File Streams
    6. String Streams and Stream and Stream Buffer Iterators
    7. Summary
  20. Chapter 10: Working with Numerical, Time, and Complex Data
    1. Numerical Calculations
    2. Numeric Algorithms
      1. Storing Incremental Values in a Range
      2. Summing a Range
      3. Inner Product
      4. Adjacent Differences
      5. Partial Sums
      6. Maxima and Minima
    3. Storing and Working with Numerical Values
      1. Basic Operations on valarray Objects
      2. Unary Operators
      3. Compound Assignment Operators for valarray Objects
      4. Binary Operations on valarray Objects
      5. Accessing Elements in valarray Objects
    4. Rational Arithmetic
    5. Temporal Templates
      1. Defining Durations
      2. Clocks and Time Points
    6. Complex Numbers
      1. Creating Objects That Represent Complex Numbers
      2. Complex Arithmetic
      3. Comparisons and Other Operations on Complex Numbers
      4. A Simple Example Using Complex Numbers
    7. Summary
  21. Index

Product information

  • Title: Using the C++ Standard Template Libraries
  • Author(s): Ivor Horton
  • Release date: October 2015
  • Publisher(s): Apress
  • ISBN: 9781484200049