C++ Algorithm transform()
C++ Algorithm transform() function is used in two different ways:
1.unary operation:- This method performs unary operation op on the elements in range [first1, last1] and stores the result in range starting from result.
This transform() applies a function to each element of a range:
2.Binary operation:- This method performs binary operation binary_op on the elements in range [first1, last1] with the elements in the range starting with iterator first2 and stores the result in range starting from result.
This transform() takes two 2 ranges and applies a function that takes 2 parameters, on each couple of elements from the input ranges:
Syntax
unary operation(1)
Binary operation(2)
Parameter
first1: An input iterator pointing the position of the first element of the first range to be operated on.
last1: An iterator pointing the position one past the final element of the first range to be operated on.
first2: Input iterator pointing to the first element in the second range to be operated on.
result: An output iterator to the initial position of the range where the operation results are stored.
op: Unary function applied to each element of the range.
binary_op: Binary function that two elements passed as its arguments.
Return value
transform() returns an iterator pointing to the end of the transformed range.