std::transform
<algorithm>
unary operation(1) | template <class InputIterator, class OutputIterator, class UnaryOperation> OutputIterator transform (InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperation op); |
---|---|
binary operation(2) | template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation> OutputIterator transform ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryOperation binary_op ); |
Transform range
Applies an operation sequentially to the elements of one (1) or two (2) ranges and stores the result in the range that begins at
result.
-
(1) unary operation
-
Applies
op to each of the elements in the range
[first1,last1)
and stores the value returned by each operation in the range that begins at result.
(2) binary operation
-
Calls
binary_op using each of the elements in the range
[first1,last1)
as first argument, and the respective argument in the range that begins at first2 as second argument. The value returned by each call is stored in the range that begins at result.
The behavior of this function template is equivalent to:
| |
The function allows for the destination range to be the same as one of the input ranges to make transformations in place.
Parameters
-
first1, last1
-
Input iterators to the initial and final positions of the first sequence. The range used is
[first1,last1)
, which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.
first2
-
Input iterator to the initial position of the second range. The range includes as many elements as
[first1,last1)
.
result
-
Output iterator to the initial position of the range where the operation results are stored. The range includes as many elements as
[first1,last1)
.
op
-
Unary function that accepts one element of the type pointed by
InputIterator as argument, and returns some result value convertible to the type pointed by
OutputIterator.
This can either be a function pointer or a function object.
binary_op
-
Binary function that accepts two elements as argument (one of each of the two sequences), and returns some result value convertible to the type pointed by
OutputIterator.
This can either be a function pointer or a function object.
Neither op nor binary_op should directly modify the elements passed as its arguments: These are indirectly modified by the algorithm (using the return value) if the same range is specified for result.
Return value
An iterator pointing to the element that follows the last element written in the result sequence.Example
| |
Output:
first contains: 21 41 61 81 101 |