# STL for C++ Programmers

## ISBN 0-471-97181-2 Leen Ammeraal Chichester: John Wiley

You can download the file stlcpp.zip, containing the programs of this book, by clicking here.

Book review, including link to Linux versions of the example programs:
http://www.linuxjournal.com/lj/issue44/2468.html/

## Contents

Preface ix
Chapter 1 STL for Beginners 1
1.1 Templates, Namespaces and Type bool 1
1.2 Starting with STL 6
1.3 Vectors, Lists and Deques 12
1.4 Sorting 16
1.5 The find Algorithm 21
1.6 The copy Algorithm and an Insert Iterator 22
1.7 The merge Algorithm 24
1.8 User-defined Types 25
1.9 Iterator Categories 27
1.10 The replace and reverse Algorithms 31
1.11 The sort Algorithm Revisited 32
1.12 Introduction to Function Objects 33
1.13 Using find_if, remove and remove_if 35
1.14 The auto_ptr Class 39

Chapter 2 More Algorithms and Containers 43
2.1 The accumulate Algorithm 43
2.2 The for_each Algorithm 45
2.3 Counting 47
2.4 STL-Provided Function Objects 48
2.5 Introduction to Associative Containers 50
2.6 Sets and Multisets 51
2.7 Maps and Multimaps 53
2.8 Pairs and Comparisons 55
2.9 Maps Revisited 57
2.10 Other insert Functions 63
2.11 Erasing Elements of a Map 64
2.12 More Convenient Strings 65

Chapter 3 Sequence Containers 71
3.1 Vectors and Related Types 71
3.2 The capacity and reserve Functions 75
3.3 Summary of vector Member Functions 78
3.4 Deques 81
3.5 Lists 83
3.6 Vectors of Vectors 90
3.7 Avoiding Explicit Memory Allocation 91

Chapter 4 Associative Containers 95
4.1 Introduction 95
4.2 Member Functions for Sets 99
4.3 Set Unions and Intersections 104
4.4 Differences Between Multisets and Sets 107
4.5 Maps 108
4.6 Multimaps 112
4.7 A Concordance 114

5.1 Stacks 119
5.2 Queues 122
5.3 Priority Queues 123

Chapter 6 Function Objects and Adaptors 127
6.1 Function Objects 127
6.2 Unary Predicates and Binders 131
6.3 Negators 132
6.4 Two Useful STL Base Classes 134
6.5 Function Objects and transform 136

Chapter 7 Generic Algorithms 143
7.1 Nonmutating Sequence Algorithms 144
7.1.1 The find, count, for_each, find_first_of and find_end Algorithms 144
7.1.3 Mismatch 147
7.1.4 Equal 149
7.1.5 Search 150
7.2 Mutating Sequence Algorithms 151
7.2.1 Transform 151
7.2.2 Copy 152
7.2.3 Rotate 154
7.2.4 Swap 155
7.2.5 Replace 157
7.2.6 Remove 160
7.2.7 Fill 160
7.2.8 Generate 161
7.2.9 Unique 163
7.2.10 Reverse 165
7.2.11 Random Shuffle 166
7.2.12 Partition 167
7.3 Sorting-Related Algorithms 169
7.3.1 Less-Than and Other Comparison Operations 169
7.3.2 Sort 169
7.3.3 Stable Sort 170
7.3.4 Partial Sort 171
7.3.5 Nth Element 173
7.3.6 Binary Search 174
7.3.7 Merge 176
7.3.8 Set Operations on Sorted Structures 178
7.3.9 Heap Operations 180
7.3.10 Minimum and Maximum 183
7.3.11 Lexicographical Comparison 185
7.3.12 Permutation Generators 186
7.4 Generalized Numeric Algorithms 188
7.4.1 Accumulate 188
7.4.2 Inner Product 189
7.4.4 Partial Sum 190