Standard C++ Library
Copyright 1998, Rogue Wave Software, Inc.
NAME
transform
- Applies an operation to a range of values in a collection
and stores the result.
SYNOPSIS
#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);
DESCRIPTION
The transform algorithm has two forms. The first form
applies unary operation op to each element of the range
[first, last), and sends the result to the output iterator
result. 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 at first2, and sends the
result to result. 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 iterator
result can be a third sequence, or either of the two input
sequences.
Formally, transform assigns through every iterator i in the
range [result, result + (last1 - first1)) a new
corresponding value equal to:
op(*(first1 + (i - result))
or
binary_op(*(first1 + (i - result), *(first2 + (i - result)))
transform returns result + (last1 - first1). op and
binary_op must not have any side effects. result may be
equal to first in case of unary transform, or to first1 or
first2 in case of binary transform.
COMPLEXITY
Exactly last1 - first1 applications of op or binary_op are
performed.
EXAMPLE
//
// 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
WARNINGS
If your compiler does not support default template parame-
ters, then you always need to supply the Allocator template
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 for std.