beautypg.com

HP Integrity NonStop H-Series User Manual

Page 149

background image

The function is generalized to iterators in the function named iter_swap(). The algorithm
swap_ranges() then extends this to entire sequences. The values denoted by the first sequence are
exchanged with the values denoted by a second, parallel sequence. The description of the
swap_ranges() algorithm is as follows:

ForwardIterator swap_ranges
(ForwardIterator first, ForwardIterator last,
ForwardIterator first2);

Parallel Sequences

The second range is described only by a starting iterator. It is assumed (but not verified) that the
second range has at least as many elements as the first range. We use both functions alone and in
combination in the example program.

void swap_example ()
// illustrate the use of the algorithm swap_ranges
{
// first make two parallel sequences
int data[] = {12, 27, 14, 64}, *datap = data;
vector aVec(4);
generate(aVec.begin(), aVec.end(), iotaGen(1));

// illustrate swap and iter_swap
swap(data[0], data[2]);
vector::iterator last = aVec.end(); last--;
iter_swap(aVec.begin(), last);

// now swap the entire sequence
swap_ranges (aVec.begin(), aVec.end(), datap);
}

This manual is related to the following products: