.\" ident @(#)transform.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH transform 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2transform\fP \ - Applies an operation to a range of values in a collection and stores the result. .SH SYNOPSIS .br #include .br template .RE .RS 1 OutputIterator .RE .RS 0 transform (InputIterator first, InputIterator last, .RE .RS 14 OutputIterator result, UnaryOperation op); .RE .RS 0 template .RE .RS 1 OutputIterator .RE .RS 0 transform (InputIterator1 first1, InputIterator1 last1, .RE .RS 14 InputIterator2 first2, OutputIterator result, .br BinaryOperation binary_op); .SH DESCRIPTION The transform algorithm has two forms. The first form applies unary operation \f2op\fP to each element of the range \f2[first, last)\fP, and sends the result to the output iterator \f2result\fP. For example, this version of transform could be used to square each element in a vector. If the output iterator (\f2result\fP) 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, \f2binary_op\fP, to corresponding elements in the range \f2[first1, last1)\fP and the range that begins at \f2first2\fP, and sends the result to \f2result\fP. 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 elements as the first sequence. Note that the output iterator \f2result\fP can be a third sequence, or either of the two input sequences. Formally, transform assigns through every iterator \f2i\fP in the range \f2[result, result + (last1 - first1))\fP a new corresponding value equal to: \f2op(*(first1 + (i - result))\fP or \f2binary_op(*(first1 + (i - result), *(first2 + (i - result)))\fP transform returns \f2result + (last1 - first1)\fP. \f2op\fP and \f2binary_op\fP must not have any side effects. \f2result\fP may be equal to \f2first\fP in case of unary transform, or to \f2first1\fP or \f2first2\fP in case of binary transform. .SH COMPLEXITY Exactly \f2last1 - first1 \fPapplications of \f2op\fP or \f2binary_op \fPare performed. .SH EXAMPLE .RE .RS 0 // .br // trnsform.cpp .br // .br #include .br #include .br #include .br #include .br #include .br .br using namespace std; .br .br int main() .RE .RS 1 { .RE .RS 0 .br //Initialize a deque with an array of ints .br int arr1[5] = {99, 264, 126, 330, 132}; .br int arr2[5] = {280, 105, 220, 84, 210}; .br deque d1(arr1+0, arr1+5), d2(arr2+0, arr2+5); .br .br //Print the original values .br cout << "The following pairs of numbers: " .RE .RS 5 << endl << " "; .RE .RS 0 deque::iterator i1; .br for(i1 = d1.begin(); i1 != d1.end(); i1++) .RE .RS 1 cout << setw(6) << *i1 << " "; .RE .RS 0 cout << endl << " "; .br for(i1 = d2.begin(); i1 != d2.end(); i1++) .RE .RS 1 cout << setw(6) << *i1 << " "; .RE .RS 0 .br // Transform the numbers in the deque to their .br // factorials and store in the vector .br transform(d1.begin(), d1.end(), d2.begin(), .RE .RS 9 d1.begin(), multiplies()); .RE .RS 0 .br //Display the results .br cout << endl << endl; .br cout << "Have the products: " << endl << " "; .br for(i1 = d1.begin(); i1 != d1.end(); i1++) .RE .RS 1 cout << setw(6) << *i1 << " "; .RE .RS 0 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br The following pairs of numbers: .RE .RS 8 99 264 126 330 132 .RE .RS 7 280 105 220 84 210 .RE .RS 0 Have the products: .br 27720 27720 27720 27720 27720 .SH WARNINGS If your compiler does not support default template parameters, then you always need to supply the \f2Allocator\fP template argument. For instance, you need to write: \f2deque >\fP instead of: \f2deque\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP.