## transform
## Prototype
template <class InputIterator, class OutputIterator, class UnaryFunction> OutputIterator transform(InputIterator first, InputIterator last, OutputIterator result, UnaryFunction op); template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryFunction> OutputIterator transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryFunction binary_op); ## Description
The first version of The second version of Note that ## DefinitionDefined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h. ## Requirements on typesFor the first (unary) version: -
`InputIterator` must be a model of InputIterator. -
`OutputIterator` must be a model of OutputIterator. -
`UnaryFunction` must be a model of UnaryFunction. -
`InputIterator` 's value type must be convertible to`UnaryFunction` 's argument type. -
`UnaryFunction` 's result type must be convertible to a type in`OutputIterator` 's set of value types.
For the second (binary) version : -
`InputIterator1` and`InputIterator2` must be models of InputIterator. -
`OutputIterator` must be a model of OutputIterator. -
`BinaryFunction` must be a model of BinaryFunction. -
`InputIterator1` 's and`InputIterator2` 's value types must be convertible, respectively, to`BinaryFunction` 's first and second argument types. -
`UnaryFunction` 's result type must be convertible to a type in`OutputIterator` 's set of value types.
## PreconditionsFor the first (unary) version : -
`[first, last)` is a valid range. -
`result` is not an iterator within the range`[first+1, last)` . [1] -
There is enough space to hold all of the elements being copied. More formally, the requirement is that
`[result, result + (last - first))` is a valid range.
For the second (binary) version: -
`[first1, last1)` is a valid range. -
`[first2, first2 + (last1 - first1))` is a valid range. -
`result` is not an iterator within the range`[first1+1, last1)` or`[first2 + 1, first2 + (last1 - first1))` . -
There is enough space to hold all of the elements being copied. More formally, the requirement is that
`[result, result + (last1 - first1))` is a valid range.
## ComplexityLinear. The operation is applied exactly ## ExampleReplace every number in an array with its negative. Calculate the sum of two vectors, storing the result in a third vector. const int N = 1000; Vector<int> V1(N); Vector<int> V2(N); Vector<int> V3(N); iota(V1.begin(), V1.end(), 1); fill(V2.begin(), V2.end(), 75); assert(V2.size() >= V1.size() && V3.size() >= V1.size()); transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), plus<int>()); ## Notes[1] The OutputIterator ## See also |