Algorithms (STL)

Standard Library algorithms can be used generically across a variety of containers. The algorithms operate on container elements only indirectly through iterators. Many algorithms operate on sequences of elements defined by pairs of iterators - one pointing to the first element of the ewquence and one pointing to one lemenet past the last element. Also, it's possible to create your own new algorithms that operate in a similar fashion so they can be used with the Standard Library containers and iterators.

Algorithms often return iterators that indicate the results of the algorithms. Algorithm find, for example, locates an element and returns an iterator to that element. If the element is not found, it returns the "one past the end" iterator that was passed in to define the end of the range to be searched, which can be tested to determine whether an element was not found. Standard Libray algorithms create yet another opportunity for reuse - using the rich collection of popular algorithms can save you much time and effort.

An algorithm can be used with containers that support at elast the algorithm's minimum iterator requirements. Some algorithms demand powerful iterators; for example, sort demands random-access iterators.

Mutating-sequence algorithms is those algorithms that result in modifications of the containers to which the algorithms are applied:


 * copy | partition | replace_copy | stable_partition |
 * copy_backward | random_shuffle | replace_copy_if | swap |
 * fill | remove | replace_if | swap_ranges |
 * fill_n | remove_copy | reverse | transform |
 * generate | remove_copy_if | reverse_copy | unique |
 * generate_n | remove_if | rotate | unique_copy |
 * iter_swap | replace | rotate_copy | |

Nonmodifying sequence algorithms is those algorithms that do not result in modifications of the containers to which they're applied:


 * adjacent_find | equal | find_end | mismatch |
 * count | find | find_first_of | search |
 * count_if | find_each | find_if | search_n |
 * from header :                                     ||||
 * accumulate | partial_sum | inner_product | adjacent_difference |

fill, fill_n, generate, generate_n
Functions fill and fill_n set every element in a range of container elements to a specific value. Functions generate and generate_n use a generator function to create values for every element in a range of container elements. The generator function takes no arguments and returns a value that can be placed in an element of the container.

equal, mismatch, lexicographical_compare
The code demonstrates comparing sequences of values for equality using algorithms equal, mismatch, and lexicographical_compare.

remove, remove_if, remove_copy, remove_copy_if
The code demonstrates removing values from a sequence with algorithms remove, remove_if, remove_copy, and remove_copy_if.

replace, replace_if, replace_copy, replace_copy_if
The code demonstrates replacing values from a sequence using algorithms replace, replace_if, replace_copy, and replace_copy_if.

Mathematical Algorithms
The code demonstrates sevaral common mathematical algorithms, including random_shuffle, count, count_if, min_element, max_element, accumulate, for_each, and transform.

Output:

It's a good practice to check that the range specified in a call to min_element is not empty and that the return value is not the "past the end" iterator.

Basic Searching and Sorting Algorithms
The code demonstrates some basic searching and sorting capabilities, including find, find_if, sort, and binary_search.

Output:

Attempting to sort a container by using an iterator other than a random-access iterator is a compilation error. Function sort requires a random_access iterator

swap, iter_swap, swap_ranges
The code demonstrates algorithms swap, iter_swap, and swap_ranges for swapping elements.

Output:

copy_backward, merge, unique, reverse
The code demonstrates the algorithms copy_backward, merge, unique, and reverse.

Output:

inplace_merge, unique_copy, reverse_copy
The code demonstrates algorithms inplace_merge, unique_copy, and reverse_copy.

Output:

Set Operations
The code demonstrates function includes, set_difference, set_intersection, set_symmetric_difference, and set_union for maniulating sets of sorted values. To demostrates that Standard Library functions can be applied to arrays and containers, this example uses only arrays (remember, a pointer into an array is a random-access iterator).

Output:

lower_bound, upper_bound, equal_range
The code demonstrates functions lower_bound, upper_bound, and equal_range.

Output:

Heapsort
The code demonstrates the Standard Library functions for performing the heapsort sorting algorithm. Heapsort is a sorting algorithm in which an array of elements is arranged into a special binary tree called a //heap//. The key features of a heap are that the largest element is always at the top of the heap and the values of the children of any node in the binary tree are alway less than or equal to that node's value. A heap arranged in this manner is often called a //maxheap//.

Output:

min, max
Algoriths min and max determine the minimum and maximum of two elements, respectively.

inner_product
Calculate the sum of the products of two sequences by taking corresponding elements in each sequence, multiplying those elements and adding the result to a total.

adjacent_difference
Beginning with the second element in a sequence, calculate the difference (using operator -) between the current and previous elements, and store the result. This first two input iterator arguments indicate the range of elements in the container and the third indicates where the results should be stored. A second version of this algoriithm takes as a fourth argument a binary function to perform a calculation between the current element and the previous element.

partial_sum
Calculate a running total (using operator +) of the values ina a sequence. The first two input iterator arguments indicate the range of elements in the container and the third indicates where the results should be stored. A second version of this algorithm takes as a fourth argument a binary function that performs a calculation between the current vlaue in the sequence and the running total.

nth_element
Use three random-access iterators to partition a range of elements. The first and last arguments represent the range of elements. The second argument is the partioning element's location. After this algorithm executes, all elements before the partioning element are greater than or equal to that element. A second version of this algorithm takes as a fourth argument a binary comparison function.

partition
Similar to nth_element, but requires less powerful bidirectional iterators, making it more flexible. It requires two bidirectional iterators indicating the range of elements to partition. The third argument is a unary predicate function that helps partition the elements so that all elements for which the predicate is true are to the left (toward the beginning of the sequence) of those for which the predicate is false. A bidirectional iterator is returned indicating the first element in the sequence for which the predicate returns false.

stable_partition
Similar to partition except that this algorithm guarantees that equivalent elements will be maintained in their original order.

next_permutation
Next lexicographical permutation of a sequence.

prev_mutation
Previous lexicographical permutation of a sequence.

rotate
Use three forward iterator arguments to rotate the sequence indicated by teh first and last argument by the number of positions indicated by subtracting the first argument from the second argument. For example, the sequence 1, 2, 3, 4, 5 rotated by two positions would be 4, 5, 1, 2, 3.

rotate_copy
Identical to rotate except that the results are stored in a separate sequence indicated by the fourth argument - an output iterator. The two sequences must have the same number of elements.

adjacent_find
Returns an input iterator indicating the first of two identical adjacent elements in a sequence. If there are no identical adjacent elements, the iterator is positioned at the end of the sequence.

search
Searches for a subsequence of elements within a sequence of elements and, if such a subsequence is found, returns a forward iterator that indicates the first element of that subsequence. If there are no matches, the iterator is positioned at the end of the sequence to be searched.

search_n
Searches a sequence of elements looking for a subsequence in which the values of a specified number of elements have a particular value and, if such a subsequence is found, returns a forward iterator that indicates the first element of that subsequence. If there are no matches, the iterator is positioned at the end of the sequence to be searched.

partial_sort
Use three random-access iterators to sort part of a sequence. The first and last arguments indicate the sequence of elements. The second argument indicates the ending location for the sorted part of the sequence. By default, elements are ordered using operator < (a binary predicate function can also be supplied). The elements from the second argument to the end of the sequence are in an undefined order.

partial_sort_copy
Use two input iterators and two random-access iterators to sort part of the sequence indicated by the two input iterator arguments. The results are stored in the sequence indicated by the two random-access iterator arguments. By default, elements are ordered using operator < (a binary predicate function can also be supplied). The number of elements sorted is the smaller of the number of elements in the result and teh number of elements in the original sequence.

stable_sort
The algorithm is similar to sort expect that all equivalent elements are maintained in their original order. This sort is O(n log n) if enough memory is available; otherwise, it's O(n(log n)^2).