In C++, transform() is a built-in STL function used to apply the given operation to a range of elements and store the result in another range. Let’s take a look at a simple example that shows the how to use this function:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v1 = {1, 5, 6, 8};
vector<int> v2(v.size());
// Incrementing all elements of vector by 1
transform(v1.begin(), v1.end(), v2.begin(),
[](int a) {
return a + 1;
});
for (auto i : v2)
cout << i << " ";
return 0;
}
Explanation: The function adds 1 to all elements of vector v and store the result in another vector v1.
This article covers the syntax, usage, and common examples of transform() method in C++:
The transform() function is defined inside <algorithm> header file has 2 implementations:
transform(first, last, res, op1); // For Unary Operation
transform(first, last, first1, res, op2); // For Binary Operation
Parameters
- first: Iterator to the first element of the first input range.
- last: Iterator to the element just after the last element of the first input range.
- first1: Iterator to the first element of second input range.
- res: Iterator to the beginning of the output range.
- op1: A unary operation to be performed.
- op2: A binary operation to be performed.
Return Value
- Returns an iterator to element just after the last element of output range.
The transform() function can be used to perform a wide variety of operations on the given range of operations. The following examples demonstrates some of the common examples that illustrates the use of this function.
Decrementing Every Value of Array by 5 (Unary Operation)
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr1[5] = {6, 7, 8, 9, 10};
int n = sizeof(arr1)/sizeof(arr1[0]);
int arr2[n];
// Decrementing every element of array by 5
transform(arr1, arr1 + n, arr2,
[](int a) {
return a - 5;
});
for (auto i : arr2)
cout << i << " ";
return 0;
}
Convert String to Lowercase (Unary Operation)
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "GEEKSFORGEEKS";
// Convert all characters to lower case
transform(s.begin(), s.end(), s.begin(),
[](char c) {
return tolower(c);
});
cout << s;
return 0;
}
Explanation: The transform() function converts all character of string s to lowercase and store the resultant string back to string s.
Adding Each Element of Vector with Another Vector Elements(Binary Operation)
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v1 = {6, 7, 8, 9, 10};
vector<int> v2 = {1, 4, 8, 9, 43};
vector<int> v(v1.size());
// Adding all elements of vector v1 and v2
transform(v1.begin(), v1.end(), v2.begin(),
v.begin(),
[](int a, int b) {
return a + b;
});
for (auto i : v)
cout << i << " ";
return 0;
}
Explanation: The transform() function adds all elements of vector v1 and v2 with one another and store the resultant vector in v.
std::for_each() is used to apply an operation to elements of a range, but it does not store the results in an output range.
transform(), on the other hand, applies an operation and stores the transformed result in a specified output range, making it suitable for generating modified sequences based on input sequences.
Yes, transform() can work in-place by specifying the same container for the input and output. For example, if you want to modify a string in place to make it lowercase.
No, transform() is designed for read-only access to the input range. Using a function that modifies the elements of the input range can lead to unexpected results.