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().
CPP
#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];
for ( int i=0; i<n; i++)
res[i] = arr1[i] + arr2[i];
for ( int i=0; i<3; i++)
cout << res[i] << " " ;
}
|
Time Complexity: O(N) , where N is size of array.
Auxiliary Space: (N)
Using transform function of STL, we can add arrays in single line.
C++
#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];
transform(arr1, arr1+n, arr2, res, plus< int >());
for ( int i=0; i<n; i++)
cout << res[i] << " " ;
}
|
transform() in C++ is used in two forms:
1. 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++
#include <bits/stdc++.h>
using namespace std;
int increment( int x) { return (x+1); }
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof (arr)/ sizeof (arr[0]);
transform(arr, arr+n, arr, increment);
for ( int i=0; i<n; i++)
cout << arr[i] << " " ;
return 0;
}
|
2. 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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
03 Jan, 2023
Like Article
Save Article