Using the C++ Standard Template Libraries

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

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. How to use Standard Library templates with your C++ applications.Understand the different types of containers that are available and what they are used for.How to define your own class types to meet the requirements of use with containers.What iterators are, the characteristics of the various types of iterators, and how they allow algorithms to be applied to the data in different types of container.How you can define your own iterator types.What the templates that define algorithms do, and how you apply them to data stored in containers and arrays.How to access hardware clocks and use them for timing execution.How to use the templates available for compute-intensive numerical data processing.How to create and use pseudo-random number generators with distribution objects.

Author(s): Ivor Horton
Publisher: Apress
Year: 2015

Language: English
Pages: 489

Contents at a Glance
Contents
About the Author
About the Technical Reviewer
Acknowledgments
Introduction
Chapter 1: Introducing the Standard Template Library
Basic Ideas
Templates
The Containers
Iterators
Obtaining Iterators
Iterator Categories
Stream Iterators
Iterator Adaptors
Reverse Iterators
Insert Iterators
Move Iterators
Operations on Iterators
Smart Pointers
Using unique_ptr Pointers
Resetting unique_ptr Objects
Comparing and Checking unique_ptr Objects
Using shared_ptr Pointers
Resetting shared_ptr Objects
Comparing and Checking shared_ptr Objects
weak_ptr Pointers
Algorithms
Passing a Function as an Argument
Function Objects
Lambda Expressions
Naming a Lambda Expression
Passing a Lambda Expression to a Function
The Capture Clause
Summary
Chapter 2: Using Sequence Containers
The Sequence Containers
Function Members That Are Common Between Containers
Using array Containers
Accessing Elements
Using Iterators with array Containers
Comparing array Containers
Using vector Containers
Creating vector Containers
The Capacity and Size of a Vector
Accessing Elements
Using Iterators with a vector Container
Adding New Elements to a vector Container
Appending Elements
Inserting Elements
Deleting Elements
vector Containers
Using deque Containers
Creating deque Containers
Accessing Elements
Adding and Removing Elements
Replacing the Contents of a deque Container
Using a list Container
Creating list Containers
Adding Elements
Removing Elements
Sorting and Merging Elements
Accessing Elements
Using forward_list Containers
Defining Your Own Iterators
STL Iterator Requirements
A Problem with Using STL Iterators
The STL Approach
Using the Iterator Template
STL Iterator Member Function Requirements
Summary
Chapter 3: Container Adapters
What Are Container Adapters?
Creating and Using a stack Container Adapter
Stack Operations
Creating and Using a queue Container Adapter
Queue Operations
A Practical Use of a Queue Container
Using a priority_queue Container Adapter
Creating a Priority Queue
Operations for a Priority Queue
Heaps
Creating a Heap
Heap Operations
Storing Pointers in a Container
Storing Pointers in Sequence Containers
Storing Pointers in a Priority Queue
Heaps of Pointers
Containers of Base Class Pointers
Applying Algorithms to a Range of Pointers
Summary
Chapter 4: Map Containers
Introducing Map Containers
Using a map Container
Creating a map Container
Inserting Elements in a map
Constructing map Elements in Place
Accessing Elements in a map
Deleting Elements
Using pair<> and tuple<> Objects
Operations with a pair
Operations with a tuple
tuples and pairs in Action
Using a multimap Container
Changing the Comparison Function
Using a greater Object
Defining Your Own Function Object for Comparing Elements
Hashing
Functions That Generate Hash Values
Using an unordered_map Container
Creating and Managing unordered_map Containers
Adjusting the Bucket Count
Inserting Elements
Accessing Elements
Removing Elements
Accessing Buckets
Using an unordered_multimap Container
Summary
Chapter 5: Working with Sets
Understanding Set Containers
Using set Containers
Adding and Removing Elements
Accessing Elements
Working with sets
Creating the Students
Creating a set of Students for a Subject
Creating the Subjects and the Courses
Enrolling Students on Courses
Checking the Students’ Courses
Outputting the Courses
The Complete Program
Set Iterators
Storing Pointers in a set Container
An Example Storing Smart Pointers in a set Container
Creating a vector of shared_ptr Elements
Outputting Objects Referenced by weak_ptr
Using Smart Pointers as Keys in a map Container
The Trouble with Comparing Smart Pointers
Using multiset Containers
Storing Pointers to Derived Class Objects
Defining the Containers
Defining main() for the Example
unordered_set Containers
Adding Elements
Retrieving Elements
Deleting Elements
Producing a Bucket List
Using unordered_multiset Containers
Operations on Sets
The set_union() Algorithm
The set_intersection( ) Algorithm
The set_difference() Algorithm
The set_symmetric_difference() Algorithm
The includes( ) Algorithm
Set Operations in Action
Summary
Chapter 6: Sorting, Merging, Searching, and Partitioning
Sorting a Range
Sorting and the Order of Equal Elements
Partial Sorting
Testing for Sorted Ranges
Merging Ranges
Searching a Range
Finding any of a Range of Elements in a Range
Finding Multiple Elements from a Range
The find_end( ) Algorithm
The search( ) Algorithm
The search_n( ) Algorithm
Par titioning a Range
The partition_copy() Algorithm
The partition_point() Algorithm
Binary Search Algorithms
The binary_search() Algorithm
The lower_bound() Algorithm
The equal_range() algorithm
Summary
Chapter 7: More Algorithms
Testing Element Properties
Comparing Ranges
Finding Where Ranges Differ
Lexicographical Range Comparisons
Permutations of Ranges
Copying a Range
Copying a Number of Elements
Conditional Copying
Reverse Order Copying
Copying and Reversing the Order of Elements
Copying a Range Removing Adjacent Duplicates
Removing Adjacent Duplicates from a Range
Rotating Ranges
Moving a Range
Removing Elements from a Range
Setting and Modifying Elements in a Range
Generating Element Values with a Function
Transforming a Range
Replacing Elements in a Range
Applying Algorithms
Summary
Chapter 8: Generating Random Numbers
What Is a Random Number?
Probability, Distributions, and Entropy
What Is Probability?
What Is a Distribution?
What Is Entropy?
Generating Random Numbers with the STL
Seeds in Random Number Generation
Obtaining Random Seeds
Seed Sequences
Distribution Classes
The Default Random Number Generator
Creating Distribution Objects
Uniform Distributions
Discrete Uniform Distributions
Applying Uniform Integer Distributions
Continuous Uniform Distributions
Using a Continuous Uniform Distribution
Creating a Standard Uniform Distribution
Normal Distributions
Using a Normal Distribution
Lognormal Distributions
Using a Lognormal Distribution
Other Distributions Related to the Normal Distribution
Sampling Distributions
The Discrete Distribution
Using a Discrete Distribution
Piecewise Constant Distributions
Piecewise Linear Distributions
Other Distributions
Poisson Distributions
Geometric Distributions
Exponential Distributions
Gamma Distributions
Weibull Distributions
Binomial Distributions
Negative Binomial Distributions
Extreme Value Distributions
Random Number Engines and Generators
The Linear Congruential Engine
Generators Based on the Linear Congruential Engine
The Mersenne Twister Engine
Generators that are Instances of the Mersenne Twister Engine
The Subtract with Carry Engine
Generators that are Instances of the Subtract with Carry Engine
Shuffling a Range of Elements
Summary
Chapter 9: Stream Operations
Stream Iterators
Input Stream Iterators
Iterators and Stream Iterators
Reading Using Input Stream Function Members
Output Stream Iterators
Writing Using Function Members of an Output Stream Iterator
Overloading the Insertion and Extraction Operators
Using Stream Iterators with Files
File Streams
File Stream Class Templates
File Input Using a Stream Iterator
Repeated File Reads Using a Stream Iterator
File Output Using a Stream Iterator
Stream Iterators and Algorithms
Stream Buffer Iterators
Input Stream Buffer Iterators
Output Stream Buffer Iterators
Using Stream Buffer Iterators with File Streams
String Streams and Stream and Stream Buffer Iterators
Summary
Chapter 10: Working with Numerical, Time, and Complex Data
Numerical Calculations
Numeric Algorithms
Storing Incremental Values in a Range
Summing a Range
Inner Product
Applying Inner Product
Defining Alternative Inner Product Operations
Adjacent Differences
Partial Sums
Maxima and Minima
Storing and Working with Numerical Values
Basic Operations on valarray Objects
Unary Operators
Compound Assignment Operators for valarray Objects
Binary Operations on valarray Objects
Accessing Elements in valarray Objects
Creating a Slice
Selecting a Row
Selecting a Column
Using a Slice
Applying Slices to Solve Equations
Gaussian Elimination
The Elimination Process
Finding the Best Pivot
Getting the Input Data
Rows as slice Objects
Finding the Best Pivot
Generating Row Echelon Form
Back Substitution
The Complete Program
Multiple Slices
Selecting Multiple Rows or Columns
Using gslice Objects
Selecting Arbitrary Subsets of Elements
Selecting Elements Conditionally
Rational Arithmetic
Temporal Templates
Defining Durations
Arithmetic Operations between Durations
Conversions between Duration Types
Comparing Durations
Duration Literals
Clocks and Time Points
Creating Time Points
Durations of Time Points
Arithmetic with Time Points
Comparing Time Points
Operations with Clocks
Timing Execution
Complex Numbers
Creating Objects That Represent Complex Numbers
Complex Arithmetic
Comparisons and Other Operations on Complex Numbers
A Simple Example Using Complex Numbers
Summary
Index