In this chapter and in Chapter 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 chapter 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 Chapter 13 (Initialization Algorithms) |
|
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 Chapter 13 (Searching Algorithms) |
|
find |
find an element matching the argument |
find_if |
find an element satisfying a condition |
adjacent_find |
find consecutive duplicate elements |
search |
match a subsequence 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 Chapter 13 (In-Place Transformations) |
|
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 Chapter 13 (Removal Algorithms) |
|
remove |
remove elements that match condition |
unique |
remove all but first of duplicate values in sequences |
scalar generating algorithms Chapter 13 (Scalar Generating Algorithms) |
|
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 Chapter 13 (Sequence Generating Algorithms) |
|
transform |
transform each element |
partial_sum |
generate sequence of partial sums |
adjacent_difference |
generate sequence of adjacent differences |
miscellaneous operations Chapter 13 (Miscellaneous Operations |
|
for_each |
apply a function to each element of collection |
In this chapter 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 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.
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>