Online Book Reader

Home Category

Professional C__ - Marc Gregoire [185]

By Root 1420 0
algorithms. With these algorithms, you should rarely need to write a for loop to iterate over a sequence of values.

Search Algorithms

Numerical Processing Algorithms

The following numerical processing algorithms are provided. None of them require the source sequences to be ordered. All of them have a linear complexity.

ALGORITHM NAME ALGORITHM SYNOPSIS

count(), count_if() Counts the number of elements matching a value or that cause a predicate to return true.

accumulate() “Accumulates” the values of all the elements in a sequence. The default behavior is to sum the elements, but the caller can supply a different binary function instead.

inner_product() Similar to accumulate(), but works on two sequences. Calls a binary function (multiplication by default) on parallel elements in the sequences, accumulating the result using another binary function (addition by default). If the sequences represent mathematical vectors, the algorithm calculates the dot product of the vectors.

partial_sum() Generates a new sequence in which each element is the sum (or other binary operation) of the parallel element, and all preceding elements, in the source sequence.

adjacent_difference() Generates a new sequence in which each element is the difference (or other binary operation) of the parallel element, and its predecessor, in the source sequence.

Comparison Algorithms

The following comparison algorithms are provided. None of them require the source sequences to be ordered. All of them have a linear worst case complexity.

ALGORITHM NAME ALGORITHM SYNOPSIS

equal() Determines if two sequences are equal by checking if parallel elements are equal or match a predicate.

mismatch() Returns the first element in each sequence that does not match the element in the same location in the other sequence.

lexicographical_compare() Compares two sequences to determine their “lexicographical” ordering. Compares each element of the first sequence with its equivalent element in the second. If one element is less than the other, that sequence is lexicographically first. If the elements are equal, compares the next elements in order.

Operational Algorithms

The following operational algorithms are provided. None of them require the source sequences to be ordered. All of them have a linear complexity.

ALGORITHM NAME ALGORITHM SYNOPSIS

for_each() Executes a function on each element in the sequence.

Modifying Algorithms

The modifying algorithms modify some or all of the elements in a sequence. Some of them modify elements in place, so that the original sequence changes. Others copy the results to a different sequence so that the original sequence is unchanged. All of them have a linear worst case complexity. The following table summarizes the modifying algorithms:

ALGORITHM NAME ALGORITHM SYNOPSIS

transform() Calls a unary function on each element of a sequence or a binary function on parallel elements of two sequences.

copy(), copy_backward() Copies elements from one sequence to another.

iota() Sequentially assigns a given value to each element in a sequence and increments the given value after each element assignment.

copy_if() Copies elements for which the predicate returns true from one sequence to another.

copy_n() Copies n elements from one sequence to another.

partition_copy() Copies elements from one sequence to two different sequences. The target sequence is selected based on the result of a predicate, either true or false.

move() Moves elements from one sequence to another. This uses the efficient move semantics introduced by C++11.

move_backward() Moves elements from one sequence to another starting with the last element. This uses the efficient move semantics introduced by C++11.

iter_swap(), swap_ranges() Swaps two elements or sequences of elements.

replace(), replace_if() Replaces all elements matching a value or that cause a predicate to return true with a new element.

replace_copy(), replace_copy_if() Replaces all elements matching a value or that cause a predicate to return true with a new element, by copying results

Return Main Page Previous Page Next Page

®Online Book Reader