Standard C++ Library Copyright 1998, Rogue Wave Software, Inc.

transform- Applies an operation to a range of values in a collection and stores the result.

#include <algorithm> template <class InputIterator, class OutputIterator, class UnaryOperation> OutputIterator transform (InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op); template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation> OutputIterator transform (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryOperation binary_op);

The transform algorithm has two forms. The first form applies unary operationopto each element of the range [first,last), and sends the result to the output iteratorresult. For example, this version of transform could be used to square each element in a vector. If the output iterator (result) is the same as the input iterator used to traverse the range, transform performs its transformation in place. The second form of transform applies a binary operation,binary_op, to corresponding elements in the range [first1,last1) and the range that begins atfirst2, and sends the result toresult. For example, transform can be used to add corresponding elements in two sequences, and store the set of sums in a third. The algorithm assumes, but does not check, that the second sequence has at least as many ele- ments as the first sequence. Note that the output iteratorresultcan be a third sequence, or either of the two input sequences. Formally, transform assigns through every iteratoriin the range [result,result+ (last1-first1)) a new corresponding value equal to:op(*(first1+ (i-result)) orbinary_op(*(first1+ (i-result), *(first2+ (i-result))) transform returnsresult+ (last1-first1).opandbinary_opmust not have any side effects.resultmay be equal tofirstin case of unary transform, or tofirst1orfirst2in case of binary transform.

Exactlylast1-first1applications ofoporbinary_opare performed.

// // trnsform.cpp // #include <functional> #include <deque> #include <algorithm> #include <iostream> #include <iomanip> using namespace std; int main() { //Initialize a deque with an array of ints int arr1[5] = {99, 264, 126, 330, 132}; int arr2[5] = {280, 105, 220, 84, 210}; deque<int> d1(arr1+0, arr1+5), d2(arr2+0, arr2+5); //Print the original values cout << "The following pairs of numbers: " << endl << " "; deque<int>::iterator i1; for(i1 = d1.begin(); i1 != d1.end(); i1++) cout <<setw(6)<< *i1 << " "; cout << endl << " "; for(i1 = d2.begin(); i1 != d2.end(); i1++) cout <<setw(6)<< *i1 << " "; // Transform the numbers in the deque to their // factorials and store in the vector transform(d1.begin(), d1.end(), d2.begin(), d1.begin(), multiplies<int>()); //Display the results cout << endl << endl; cout << "Have the products: " << endl << " "; for(i1 = d1.begin(); i1 != d1.end(); i1++) cout <<setw(6)<< *i1 << " "; return 0; } Program Output The following pairs of numbers: 99 264 126 330 132 280 105 220 84 210 Have the products: 27720 27720 27720 27720 27720

If your compiler does not support default template parame- ters, then you always need to supply theAllocatortemplate argument. For instance, you need to write:deque<int,allocator<int> > instead of:deque<int> If your compiler does not support namespaces, then you do not need the using declaration forstd.