|
 |
 |
[an error occurred while processing this directive]
CHAPTER 9 THE STANDARD TEMPLATE LIBRARYS INCLUDED ALGORITHMS
How do I...
- 9.1 Create classes for sequential containers?
- 9.2 Use predicates with sequence operations?
- 9.3 Repeat an action with all elements in a container range?
- 9.4 Compare two sequences?
- 9.5 Search for a sequence of values in a container?
- 9.6 Accumulate all container elements and create a sequence of accumulated sums?
- 9.7 Sort elements in a container using different sorting indexes?
- 9.8 Change the order of the container elements?
The Standard Template Library (STL) is a C++ component library developed by Alexander Stepanov and Meng Lee at the Hewlett-Packard laboratories. Algorithms, is one of the sets of STL components. An algorithm is a computational procedure that applies to different containers.
STL algorithms are represented by template functions and provide copying, searching, sorting, merging, and other operations on data. Algorithms are not member functions; they are separate from the container classes.
The idea of separating data and algorithms might sound strange for a programmer used to the object-oriented approach to software design. Therefore, understanding the STL algorithms might be a serious challenge.
It is common now to combine STL algorithms into four groups: nonmutating operations, mutating operations, sorting and related operations, and generalized numeric operations. Tables 9.1 through 9.4 briefly describe the algorithms.
Table 9.1 Nonmutating Operations
Algorithm
| Description
|
adjacent_find
| Finds the consecutive duplicates in a range of elements
|
count, count_if
| Counts the number of elements that satisfy a condition in a range of elements
|
equal
| Compares two sequence ranges element by element
|
for_each
| Applies a function to all elements in a range
|
find, find_if
| Finds an element that equals a specified value or satisfies a certain condition
|
mismatch
| Finds mismatches between two ranges of elements
|
search
| Searches for the first match of a sub-sequence in a sequence of data
|
Table 9.2 Mutating Operations
Algorithm
| Description
|
copy, copy_backward
| Copies (or copies backward) a range of elements into another range
|
fill, fill_n
| Fills a range of elements with a specified value
|
generate, generate_n
| Generates values and fills a range with the values
|
partition, stable_partition
| Moves the elements that satisfy a predicate before the other elements in a sequence
|
random_shuffle
| Randomly shuffles data in a range of elements
|
remove, remove_if, remove_copy,
| Removes elements from a
|
remove_copy_if
| range of elements
|
replace, replace_if,
| Replaces elements that satisfy specified
|
replace_copy, replace_copy_if
| conditions in a range of elements
|
reverse, reverse_copy
| Reverses data in a range of elements
|
rotate, rotate_copy
| Rotates data in a range of elements
|
swap, iter_swap, swap_ranges
| Exchanges values or ranges in a sequence
|
transform
| Performs a specified operation on a sequence, or two sequences, and copies the result into a new sequence
|
unique, unique_copy
| Removes consecutive duplicates from a range of elements
|
Table 9.3 Sorting and Related Operations
Algorithm
| Description
|
sort, stable_sort, partial_sort,
| Sorts a range of elements (unstable,
|
partial_sort_copy
| stable, or partial)
|
nth_element
| Places an element in the location where it would be sorted
|
lower_bound, upper_bound,
| Finds bounds for, or location of,
|
equal_range, binary_search
| a value in a sorted range
|
merge, inplace_merge
| Combines two sorted ranges in a single sorted range
|
includes, set_union,
| Sets different operations on sorted ranges
|
set_intersection,
|
|
set_difference,
|
|
set_symmetric_difference
|
|
push_heap, pop_heap,
| Performs heap operations
|
make_heap, sort_heap
|
|
min, max, min_element,
| Finds minimum and maximum of two arguments
|
max_element
| or in a specified range
|
lexicographical_compare
| Lexicographically compares two ranges of elements
|
next_permutation,
| Generates permutation of a range of elements
|
prev_permutation
|
|
|
|