std::transform() in C++ STL (Perform an operation on all elements)
Consider the problem of adding contents of two arrays into a third array. It is given that all arrays are of same size.
Following is simple C++ program without transform().
// A C++ code to add two arrays #include <bits/stdc++.h> using namespace std; int main() { int arr1[] = {1, 2, 3}; int arr2[] = {4, 5, 6}; int n = sizeof(arr1)/sizeof(arr1[0]); int res[n]; // Code to add two arrays for (int i=0; i<n; i++) res[i] = arr1[i] + arr2[i]; for (int i=0; i<3; i++) cout << res[i] << " "; } |
chevron_right
filter_none
Output :
5 7 9
Using transform function of STL, we can add arrays in single line.
// Using tansform() in STL to add two arrays #include <bits/stdc++.h> using namespace std; int main() { int arr1[] = {1, 2, 3}; int arr2[] = {4, 5, 6}; int n = sizeof(arr1)/sizeof(arr1[0]); int res[n]; // Single line code to add arr1[] and arr2[] and // store result in res[] transform(arr1, arr1+n, arr2, res, plus<int>()); for (int i=0; i<n; i++) cout << res[i] << " "; } |
chevron_right
filter_none
Output :
5 7 9
transform() in C++ is used in two forms:
- Unary Operation : Applies a unary operator on input to convert into output
transform(Iterator inputBegin, Iterator inputEnd, Iterator OutputBegin, unary_operation)Following is C++ example.
// C++ program to demonstrate working of// transform with unary operator.#include <bits/stdc++.h>usingnamespacestd;intincrement(intx) {return(x+1); }intmain(){intarr[] = {1, 2, 3, 4, 5};intn =sizeof(arr)/sizeof(arr[0]);// Apply increment to all elements of// arr[] and store the modified elements// back in arr[]transform(arr, arr+n, arr, increment);for(inti=0; i<n; i++)cout << arr[i] <<" ";return0;}chevron_rightfilter_noneOutput :
2 3 4 5 6
- Binary Operation : Applies a binary operator on input to convert into output
transform(Iterator inputBegin1, Iterator inputEnd1, Iterator inputBegin2, Iterator OutputBegin, binary_operation)The example mentioned above for adding two arrays is an example of transform with binary operation.
More examples:
We can use transform to convert a string to upper case (See this)
We can modify above examples for vectors also.
// vect is a vector of integers.
transform(vect.begin(), vect.end(),
vect.begin(), increment);
Related Topic:
Functors in C++
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- Sum of all elements between k1'th and k2'th smallest elements
- Fibonacci sum of a subset with all elements <= k
- Get first and last elements from Array and Vector in CPP
- How to insert elements in C++ STL List ?
- Delete elements in C++ STL list
- How to find the sum of elements of an Array using STL in C++?
- How to find the sum of elements of a Vector using STL in C++?
- Find elements of an Array which are Odd and Even using STL in C++
- Elements present in first array and not in second using STL in C++
- Remove duplicate elements in an Array using STL in C++
- Sum of array Elements without using loops and recursion
- Maximum Subarray Sum after inverting at most two elements
- Find elements of an array which are divisible by N using STL in C++
- How to find common elements between two Arrays using STL in C++?
- How to find common elements between two Vector using STL in C++?



