Algorithm
| Purpose
|
Algorithms initializing a sequence
|
copy()
| Copies a sequence into another sequence
|
copy_backward()
| Copies a sequence into another sequence
|
fill()
| Fills a sequence with an initial value
|
fill_n()
| Fills n positions with an initial value
|
generate()
| Initializes a sequence using a generator
|
generate_n()
| Initializes n positions using a generator
|
swap()
| Exchanges values
|
swap_ranges()
| Swaps values from two parallel sequences
|
Searching algorithms
|
adjacent_find()
| Finds consecutive duplicate elements
|
find()
| Finds an element matching the argument
|
find_end()
| Finds the last occurrence of a sub-sequence in a sequence
|
find_first_of()
| Finds the first occurrence of one member of a sequence in another sequence
|
find_if()
| Finds an element satisfying a condition
|
max_element()
| Finds the maximum value in a sequence
|
min_element()
| Finds the minimum value in a sequence
|
mismatch()
| Finds first mismatch in parallel sequences
|
search()
| Matches a sub-sequence within a sequence
|
Algorithms for in-place transformations
|
inplace_merge()
| Merges two adjacent sequences into one
|
next_permutation()
| Generates permutations in sequence
|
partition()
| Partitions elements into two groups
|
permutation()
| Generates successive permutations of a sequence based on an ordering function
|
prev_permutation()
| Generates permutations in reverse sequence
|
random_shuffle()
| Randomly rearranges elements in a sequence
|
replace()
| Replaces specific values with new value
|
replace_if()
| Replaces elements matching predicate
|
reverse()
| Reverses the elements in a sequence
|
rotate()
| Rotates elements in a sequence around a point
|
stable_partition()
| Partitions preserving original ordering
|
Removal algorithms
|
remove()
| Removes elements that match condition
|
unique()
| Removes all but first of duplicate values in sequences
|
Scalar-producing algorithms
|
accumulate()
| Reduces sequence to a scalar value
|
count()
| Counts number of elements matching value
|
count_if()
| Counts elements matching predicate
|
equal()
| Checks two sequences for equality
|
inner_product()
| Gives inner product of two parallel sequences
|
lexicographical_compare()
| Compares two sequences
|
Sequence-generating algorithms
|
adjacent_difference()
| Generates sequence of adjacent differences
|
partial_sum()
| Generates sequence of partial sums
|
transform()
| Transforms each element
|
Miscellaneous operations
|
for_each()
| Applies a function to each element of a collection
|
In this chapter, we 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 chapters on the various container classes. These cross references have been noted where appropriate.
All the short example programs described in this section are contained in a number of files, named alg1.cpp through alg6.cpp. In the files, the example programs are augmented with output statements describing the test programs and illustrating the results of executing the algorithms. So as not to confuse the reader with unnecessary detail, we have generally omitted these output statements from the descriptions here. If you wish to see the test programs complete with output statements, you can compile and execute the 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 std::accumulate(), std::inner_product(), std::partial_sum(), and std::adjacent_difference() are defined in the header file numeric.