Transform in C++ STL

About Transform in STL

On this page we will discuss about library function transform() in STL which is used in C++. The Standard Template Library(STL) is a set of complete data structures and functions which can be used in C++. The transform() function in C++ is a function in the algorithm header that is used to perform operations / transformations over all the elements within a range.

Transform in C++ STL

Sort in STL in C++

  • In C++ programming language, the transform() function is included in algorithm header file.
  • Transform function in C++ is used to perform operations on all the elements which could be of any type.
  • Suppose, we need to multiply every element of an array by a particular number, then we can make use of Transform function.

Syntax

For Unary operations :

transform(Iterator inputBegin, Iterator inputEnd,Iterator OutputBegin, unary_operation); 

For Binary operations :

transform(Iterator inputBegin1, Iterator inputEnd1,Iterator inputBegin2, 
Iterator OutputBegin, binary_operation);

where inputBegin and inputEnd are the beginning and ending iterators, respectively, of the range to be transformed, OutputBegin specifies the location where the transformed values need to be stored and operation specifies what transformation operation is to be performed and then records the resulting value at the desired location provided by the user.

Parameters

The transform() function in C++ has the following parameters:

ParameterDescription
inputBeginIt is an iterator pointing to the first element in the range to be transformed.
inputEndIt is an iterator pointing to the element one past the last element in the range to be transformed.
OutputBeginIt is an iterator pointing to the first index of the ouput where the transformed values will be stored.
operationIt specifies about what transformation is to be performed.

Implementation of STL Function Transform() in C++

Example 1:

Run
#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());
 for (int i=0; i<n; i++)
   cout="" <<="" res[i];
}<n; i++)="" cout="" <<="" res[i]="" "="" ";="" }="" <="" pre="">
</n;>

Output:

5 7 9

In the above program, we take two arrays and then form a new array using adding the corresponding values of the two given arrays using the transform function.

Example 2:

Run

#include<bits/stdc++.h> 
using namespace std;
int increment(int x) {
return (x+2);
}
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;
}

Output:

3 4 5 6 7  

In the above program, we take the array and apply an unary operation over the array elements using transform, and print the resulting values.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription