Chapter 24 - Standard Template Library - Iterators and Algorithms


Chapter Goals



24.1 Loose Coupling Produces a Strong Library


24.1 Loose Coupling Produces a Strong Library (cont.)


24.2 Iterators


24.2 Iterators (cont.)


24.2 Iterators (cont.)


24.2.1 Varieties of Iterators


24.2.1 Varieties of Iterators - Table

Iterators Operations
Input Iterator ==, !=, ++, and * (only for returning a value)
Output Iterator ==, !=, ++, and * (only for assignment)
Forward Iterator ==, !=, ++, and *
Bidirectional Iterator ==, !=, ++, --, and *
Random Access Iterator ==, !=, ++, --, [], *, as well as iter + n and iter - iter

24.3 Functions, Generators, and Predicates


24.3 Functions, Generators, and Predicates (cont.)


24.3.1 Predicates


24.3.1 Predicates (cont.)


24.3.1 Predicates (cont.)


24.3.2 Generators


24.3.2 Generators (cont.)


24.3.3 Function Objects


24.3.3 Function Objects (cont.)


24.3.3 Function Objects (cont.)

RandomInt::RandomInt(int ia, int ib)
{
   a = ia;
   b = ib;
}

inline int RandomInt::operator()()
{
   return a + rand() % (b - a + 1);
}

24.3.3 Function Objects (cont.)


24.3.3 Function Objects (cont.)


24.3.3 Function Objects (cont.)


24.3.3 Function Objects (cont.)


24.3.3 Function Objects (cont.)


24.3.3 Function Objects (cont.)


24.4 Generic Algorithms


24.4 Generic Algorithms (cont.)


24.4.1 Initialization Algorithms


24.4.1 Initialization Algorithms - fill


24.4.1 Initialization Algorithms - copy


24.4.1 Initialization Algorithms - generate


24.4.2 Transformations


24.4.2 Transformations - sort


24.4.2 Transformations - reverse


24.4.2 Transformations - random_shuffle


24.4.2 Transformations - rotate


24.4.2 Transformations - partition


24.4.2 Transformations - transform


24.4.2 Transformations - transform (cont.)


24.4.2 Transformations - transform (cont.)


24.4.2 Transformations - merge


24.4.2 Transformations - merge (cont.)


24.4.2 Transformations - next_permutation


24.4.2 Transformations - next_permutation (cont.)

vector<int> a(4);
   // Initialize 1, 2, 3, 4
generate(a.begin(), a.end(), SequenceGenerator(1));
while (next_permutation(a.begin(), a.end()))
{
   cout << "Output permutation ";
   vector<int>::iterator p = a.begin();
   while (p != a.end())
   {
      cout << *p << " ";
      ++p;
   }
   cout << "\n";
}

24.4.3 Searching Algorithms


24.4.3 Searching Algorithms - max_element and min_element


24.4.3 Searching Algorithms - find


24.4.3 Searching Algorithms - find (cont.)


24.4.3 Searching Algorithms - find_if


24.4.3 Searching Algorithms - binary_search, upper_bound, lower_bound


24.4.3 Searching Algorithms - binary_search (cont.)

vector<int> a(20);
generate(a.begin(), a.end(), RandomInt(1, 10));
sort(a.begin(), a.end());
if (binary_search(a.begin(), a.end(), 7))
{
   vector<int>::iterator p
      = lower_bound(a.begin(), a.end(), 7);
   vector<int>::iterator q
      = upper_bound(a.begin(), a.end(), 7);
   while (p != q)
   {
      cout << "found " << *p << "\n";
      ++p;
   }
}

24.4.3 Searching Algorithms (cont.)


24.4.4 Removal and Replacement Algorithms


24.4.4 Removal and Replacement Algorithms - remove_copy


24.4.4 Removal and Replacement Algorithms - remove_copy (cont.)


24.4.4 Removal and Replacement Algorithms - remove_copy_if


24.4.4 Removal and Replacement Algorithms - resizing


24.4.4 Removal and Replacement Algorithms - remove


24.4.4 Removal and Replacement Algorithms - remove_if


24.4.4 Removal and Replacement Algorithms - remove_if (cont.)


24.4.4 Removal and Replacement Algorithms - unique


24.4.4 Removal and Replacement Algorithms - replace


24.4.5 Other Algorithms


24.4.5 Other Algorithms (cont.)


24.5 Inserters


24.5 Inserters (cont.)


24.6 Stream Iterators - Output Stream Iterators


24.6 Stream Iterators - Output Stream Iterators


24.6 Stream Iterators - Output Stream Iterators


24.7 Standard Function Objects, Predicates, and Binders


24.7 Standard Function Objects, Predicates, and Binders (cont.)


24.7 Standard Function Objects, Predicates, and Binders (cont.)


24.7 Standard Function Objects, Predicates, and Binders (cont.)


24.7 Standard Function Objects, Predicates, and Binders (cont.)


24.7 Standard Function Objects, Predicates, and Binders (cont.)


24.8 Case Study: File Merge Sort

Problem:
Create a list of all words in a document, removing duplicates
Caveat:
Document too large to hold in memory
Solution:
Merge file sort, in 2 phases

24.8 Case Study: File Merge Sort (cont.)

Phase I

  1. Decide on unit size that can be held in memory (1,000 words)
  2. Read 1,000 words
  3. Sort
  4. Remove duplicates
  5. Write result to temporary file
  6. Store filename in queue
  7. If more input, goto 1

24.8 Case Study: File Merge Sort (cont.)

Phase II

  1. Remove 2 files from queue, open them
  2. Merge contents (can be done holding a few elements in memory), writing to new temporary file
  3. Delete 2 old files
  4. Make another pass over input, removing duplicates
  5. Add new file to end of queue
  6. If there's more than 1 file on the queue, goto 1
  7. Copy final file to stdout

24.8 Case Study: File Merge Sort (cont.)

Notes on implementation - Phase I


24.8 Case Study: File Merge Sort (cont.)

Notes on implementation - Phase II


24.8 Case Study: File Merge Sort (filesort.cpp)


Chapter Summary


  1. Iterators are high-level abstractions, serve the same role as pointers, can be applied to a variety of data structures. Each STL container provides a pair of member functions, begin and end, that return a starting and ending iterator.
  2. Pairs of iterators can be used to refer to a complete collection of values, or a subportion of a collection.
  3. Through the use of iterators, generic algorithms can be made to work with a variety of containers.
  4. Many generic algorithms take functions or function objects as argument. A function object is a class that implements the function call operator, and hence can be used in the same manner as a function.

Chapter Summary (cont.)


  1. Some generic algorithms take generators or predicates as arguments. A generator is a function or function object that returns a different value each time it is called. A predicate is a function or function object that returns a Boolean value.
  2. A large number of generic algorithms are provided in the standard library. Generic algorithms can be used to initialize a container, transform a collection, search for a value within a container, remove elements from a container, or other tasks.
  3. Many algorithms assign values to a container using an iterator. An inserter can be used to replace this action with an insertion into a container. Inserters can use either push_front or push_back insertion.