beautypg.com

Other operations – HP Integrity NonStop H-Series User Manual

Page 74

background image

value one. The version of transform() that manipulates two parallel sequences can be used in a similar
fashion.

transform(list_ten.begin(), list_ten.end(),
list_ten.begin(), bind1st(plus(), 1));

Similarly, the functions replace() and replace_if() (Section 12.4.2) can be used to replace elements of
a list with specific values. Rotations (

Chapter 13:

Rotate Elements Around a Midpoint

) and partitions

(

Chapter 13:

Partition a Sequence...

), can also be performed with lists.

// find the location of the value 5, and rotate around it
location = find(list_ten.begin(), list_ten.end(), 5);
rotate(list_ten.begin(), location, list_ten.end());
// now partition using values greater than 7
partition(list_ten.begin(), list_ten.end(),
bind2nd(greater(), 7));

The functions next_permutation() and prev_permutation() (

Chapter 13:

Generate Permutations in

Sequence

) can be used to generate the next permutation (or previous permutation) of a collection of

values.

next_permutation (list_ten.begin(), list_ten.end());

Other Operations

The algorithm for_each() (

Section 13:

Apply a Function to All Elements...

) will apply a function to every

element of a collection. An illustration of this use will be given in the radix sort example program in
the section on the deque data structure.

The accumulate() generic algorithm reduces a collection to a scalar value (see

Chapter 13:

Reduce

Sequence to a Single Value

). This can be used, for example, to compute the sum of a list of numbers. A

more unusual use of accumulate() will be illustrated in the radix sort example.

cout << "Sum of list is: " <<
accumulate(list_ten.begin(), list_ten.end(), 0) << endl;

Two lists can be compared against each other. They are equal if they are the same size and all
corresponding elements are equal. A list is less than another list if it is lexicographically smaller (see

Chapter 13:

Generalized Inner Product

).

This manual is related to the following products: