- C++ Standard Algorithms Library <algorithm>
- : Information on the rest of the C++ Standard Library or STL
- : Doing it the hard way
- : Glossary for the C++ Standard Library
- : Search
- : Scan, compare, and count
- : Copy, Move and swap
- : Change and Delete
- : Generate and Fill
- : Sort
- : Search, Merge and Permute Sorted Containers
- : Set Operations: union, intersection, complement,...
- : Numeric algorithms
- : Heaps
- : Examples of use
- : Specifications of Algorithms on WWW
- : Further Reading

- binary_function::=A function that has two arguments f(x,y).
- binary_predicate::=a function or function_object that returns a bool value when given two items, used for testing to see if items match, or that they are in order.
- function::=Something f that can be called like this f(x) for an argument x.
- function_object::=any instance of a class that has defined and operator() and so can be called as if it was a function.
- interval::=a range.
- iterator::=an object that indicates an item in a container,
- iterators::=plutral of iterator.
- predicate::=a function or function_object that returns a bool value when given an item.
- range::= a sequence of items in a container given by a pair of iterators indicating the beginning and the point after the last one.
## Search

(find): looks for a value in a range.

(find_if): looks for items in a range that satisfy a predicate.

(find_first_of): looks for items in first range that is also in the second range or uses a binary_predicate to find first matching item.

(find_end): looks backward for items in first range that are not also in the second range or uses a binary_predicate to find first non_matching item.

(adjacent_find): looks for first pair in range that are equal, or match under a binary_predicate.

(max): returns larger of two items, possible using a binary_predicate.

(max_element): finds largest item in a range, may use a binary_predicate. [ timeSelectionSort.cpp ]

(min): returns larger of two items, possible using a binary_predicate.

(min_element): finds largest item in a range, may use a binary_predicate.

(mismatch): search two parallel ranges and returns position of the first one that is unequal or doesn't satisfy a binary_predicate.

(search): look in first range for an occurrence of the second range, possibly using a binary_predicate.

(search_n): look in range for an occurrence of n items equal to a value, possibly using a binary_predicate.## Scan, compare, and count

(count): scan range and count occurrence of a value.

(count_if): scan range and count times a predicate is true.

(equal): test if a range equals, element another parallel range, possibly using a binary_predicate.

(for_each): Apply a function to every item in a range.## Copy, Move and swap

(copy): Copy items in range to another place indicate by its start. There is a surprising way to read and write data to/from an input/output stream by using copy and adapters that give iterators accessing a stream. For example, suppose we have a range [begin, end) ints and want to output them separated by tabs we can write:copy(begin, end, ostream_iterator<int> (cout, "\t") );

To read ints into a vector v we would writecopy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(v));

(copy_backward): Copy items in range to another place indicated by its end.

(swap): swaps values of two given variables.

(iter_swap): swaps two items in a container indicated by iterators. Used in [ timeSelectionSort.cpp ] [ timeQuickSort.cpp ]

(swap_ranges): interchanges value between two ranges.

(reverse): places the elements in the reverse order.

(reverse_copy): creates a backwards copy of a range.

(rotate): given a middle point in a range, reorganizes range so that middle comes first...

(rotate_copy): creates a rotated copy.

(partition): takes a range and reorganizes the items so that a predicate is true at first and then false... A key part of QuickSort! [ timeQuickSort.cpp ]

(stable_partition): takes a range and reorganizes the items so that a predicate is true at first and then false... but in each part the items are still in the same sequence (with some gaps).

(random_shuffle): shuffles a range, you can supply your own random number generator.## Change and Delete

(replace): scan a range and replace given old values by given new value.

(replace_if): scan a range and replace given old values by given new value IF a predicate is true.

(replace_copy): make a copy of a range but replace given old values by given new value.

(replace_copy_if): make a copy of a range but replace given old values by given new value IF a predicate is true.

(remove): deletes items in a range that equal a given value.

(remove_if): deletes items in a range if a predicate is true.

(remove_copy): makes a copy of items in a range but not those with a given value.

(remove_if): makes a copy of items in a range but not those where a predicate is true.

(unique): deletes duplicated items, leaves the first. Can use a binary_predicate.

(unique_copy): copies range but not duplicated items, leaves the first. Can use a binary_predicate.## Generate and Fill

(fill): change a range to all have the same given value.

(fill_n): change n items to all have the same given value.

(generate): change items in a range to be values produced by a function_object.

(generate_n): change n items to be values produced by function_object.

(transform): scans a range and for each use a function to generate a new object put in a second container, OR takes two intervals and applies a binary operation to items to generate a new container.## Sort

(sort): reorganize a range to be in order, can use a binary_predicate. For example: [ timeVectorSort.cpp ]Also some containers have a sort member function: [ timeListSort.cpp ]

(stable_sort): like sort but equivalent items are kept in the same sequence.

(partial_sort): Sorts part of a range.

(partial_sort_copy): makes a copy of a range but with part sorted.

(nth_element): Sorts out just the nth element!## Search, Merge and Permute Sorted Containers

(binary_search): search a sorted range for a value.

(lower_bound): finds first place in a sorted range which is not less than a given value.

(upper_bound): finds first place in a sorted range which is not greater than a given value.

(equal_range): finds a range that brackets a given value.

(merge): Combines two sorted ranges to give a new sorted range both all their items.

(inplace_merge): Combines two sorted halves of a range to give a sorted range both all their items. [ timeMergeSort.cpp ]

(next_permutation): permutes items in a range... will generate each possible order once until it returns false.

(prev_permutation): undoes next_permutation## Set Operations: union, intersection, complement,...

[ timeMultisetSort.cpp ]

(includes): set theoretic test.

(set_union ): set theoretic operation.

(set_intersection): set theoretic operation.

(set_difference): set theoretic operation.

(set_symmetric_difference): set theoretic operation.## Numeric algorithms

(accumulate): adds up items in range starting with given initial value, can also do any binary operation if it is given as an function of two arguments(binary_function).Used to code the math Σ and Π symbols.

(inner_product): the heart of linear algebra, but can be generalized to do other things by supplying two binary_function s.

(partial_sum): scans range and replaces items by sum so far, can use general binary_function.

(adjacent_difference): Scans range and replace items by differences## Heaps

The big lumps rise to the top. Seriously! A heap is an array of vector where the first item is always the biggest tiem. Further, the next two smallest items are in the second and third places. After the second you have the fourth and fithf, and after the third the sixth and seventh. As a rule the n'th item is larger than the 2*n'th and (2*n+1)'th items. For more take CSCI330 Data Structures.

(make_heap): rearranges a range so that it becomes a heap.

(sort_heap): takes a heap and creates a sorted container from it (by popping items).Combining make_heap and sort_heap gives a pretty good [O(n log n)] sort: [ timeHeapSort.cpp ]

(push_heap): puts a new element into a heap and rearranges it to still be a heap.

(pop_heap): removes the top/largest element and rearranges to leave a heap behind.## Examples of use

[ http://csci.csusb.edu/dick/examples/ ]## Specifications of Algorithms on WWW

Only the draft but close to the real thing: [ lib-algorithms.html ] (also nearly unreadable without working on it).## Further Reading

- Scott Meyers
- Effective STL: 50 specific ways to improve your use of the standard Template Library
- Addison Wesley 2001 QA76.73.C153.M49 ISBN 0-201-74962-9

. . . . . . . . . ( end of section C++ Standard Algorithms Library <algorithm>) <<Contents | End>>