## 13.1 Overview

In this section and in section 14 we will examine and illustrate each of the generic algorithms provided by the standard library. The names and a short description of each of the algorithms in this section are given in the following table. We have divided the algorithms into several categories, based on how they are typically used. This division differs from the categories used in the C++ standard definition, which is based upon which algorithms modify their arguments and which do not.

 Name Purpose algorithms used to initialize a sequence - Section 13.2 fill fill a sequence with an initial value fill_n fill n positions with an initial value copy copy sequence into another sequence copy_backward copy sequence into another sequence generate initialize a sequence using a generator generate_n initialize n positions using a generator swap_ranges swap values from two parallel sequences searching algorithms - Section 13.3 find find an element matching the argument find_if find an element satisfying a condition adjacent_find find consecutive duplicate elements find_first_of find the first occurrence of one member of a sequence in another sequence find_end find the last occurrence of a sub-sequence in a sequence search match a sub-sequence within a sequence max_element find the maximum value in a sequence min_element find the minimum value in a sequence mismatch find first mismatch in parallel sequences in-place transformations - Section 13.4 reverse reverse the elements in a sequence replace replace specific values with new value replace_if replace elements matching predicate rotate rotate elements in a sequence around a point partition partition elements into two groups stable_partition partition preserving original ordering next_permutation generate permutations in sequence prev_permutation generate permutations in reverse sequence inplace_merge merge two adjacent sequences into one random_shuffle randomly rearrange elements in a sequence removal algorithms - Section 13.5 remove remove elements that match condition unique remove all but first of duplicate values in sequences scalar generating algorithms - Section 13.6 count count number of elements matching value count_if count elements matching predicate accumulate reduce sequence to a scalar value inner_product inner product of two parallel sequences equal check two sequences for equality lexicographical_compare compare two sequences sequence generating algorithms - Section 13.7 transform transform each element partial_sum generate sequence of partial sums adjacent_difference generate sequence of adjacent differences miscellaneous operations - Section 13.8 for_each apply a function to each element of collection

In this section we will illustrate the use of each algorithm with a series of short examples. Many of the algorithms are also used in the sample programs provided in the sections on the various container classes. These cross references have been noted where appropriate.

All of the short example programs described in this section have been collected in a number of files, named alg1.cpp through alg6.cpp. In the files, the example programs have been augmented with output statements describing the test programs and illustrating the results of executing the algorithms. In order to not confuse the reader with unnecessary detail, we have generally omitted these output statements from the descriptions here. If you wish to see the text programs complete with output statements, you can compile and execute these test files. The expected output from these programs is also included in the distribution.

### 13.1.1 Include Files

To use any of the generic algorithms you must first include the appropriate header file. The majority of the functions are defined in the header file algorithm. The functions accumulate(), inner_product(), partial_sum(), and adjacent_difference() are defined in the header file numeric.

```   # include <algorithm>
# include <numeric>```