beautypg.com

Sequence-generating algorithms, Transform one or two sequences, Chapter 13 – HP Integrity NonStop H-Series User Manual

Page 173: Sequence generating algorithms

background image

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

Sequence-Generating Algorithms

Obtaining the Source

The algorithms described in this section are all used to generate a new sequence from an existing
sequence by performing some type of transformation. In most cases, the output sequence is
described by an output iterator. This means these algorithms can be used to overwrite an existing
structure (such as a

vector

). Alternatively, by using an insert iterator (see

Chapter 2

), the

algorithms can insert the new elements into a variable length structure, such as a

set

or

list

.

Finally, in some cases which we will note, the output iterator can be the same as one of the
sequences specified by an input iterator, thereby providing the ability to make an in-place
transformation.

The functions partial_sum() and adjacent_difference() are declared in the header file numeric,
while the other functions are described in the header file algorithm.

Transform One or Two Sequences

The algorithm transform() is used either to make a general transformation of a single sequence, or
to produce a new sequence by applying a binary function in a pair-wise fashion to corresponding
elements from two different sequences. The general definition of the argument and result types are
as follows:

OutputIterator transform (InputIterator first, InputIterator last,
OutputIterator result, UnaryFunction);

OutputIterator transform
(InputIterator first1, InputIterator last1,
InputIterator first2, OutputIterator result, BinaryFunction);

The first form applies a unary function to each element of a sequence. In the example program
given below, this is used to produce a vector of integer values that hold the arithmetic negation of
the values in a linked list. The input and output iterators can be the same, in which case the
transformation is applied in-place, as shown in the example program.

The second form takes two sequences and applies the binary function in a pair-wise fashion to
corresponding elements. The transaction assumes, but does not verify, that the second sequence
has at least as many elements as the first sequence. Once more, the result can either be a third
sequence, or either of the two input sequences.

This manual is related to the following products: