Std::transform function exists in C STL. We must encompass the header file to utilize this function. This method is utilized to carry out an operation on all components. It applies operations to one or more array components in sequence and saves the output in the resultant array. This approach works in two ways. These are:

Unary operation: This is applied to every item of the input series, and the output is stored in another array.

Binary operation: This operation is applied to every item of the first input series and the corresponding component of the second input series. Like the unary operation, the result is also saved in the output array.

Neither unary operation nor binary operation directly changes the component passed as a parameter. If the output specifies the same series, they are altered indirectly by the algorithm. Let’s discuss the std::transform function and its modes in detail.

Unary Operation:

Std::transform relates the specified function to one range and saves the output in another range. This technique executes a unary operation on the components in the series (array, array 12) and then saves the output in the series starting with the result.

The subsequent program shows how to perform unary operations on an array. The transform() function receives a pointer to the initial and final positions of an array and the initial position of the resultant array.

#include

#include

using namespace std;

int square(int m) {

   


   return m*m;

}

int main(int lmn, char **pqr) {

   int array[12] = {4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26};

   int result[12];

   transform(array, array 12, result, square);

   for(int j = 0; j<12; j ) {

      cout <<result[j] <<n;

   }

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/1-17.jpg" data-lazy- height="237" src="data:image/svg xml,” width=”572″>

By including the libraries, we are going to start the program. Header file # include is for input and output purposes. Therefore, the header file # include is used for the transform() function. We define the data type integer to the unary operation. Also, we define an integer data type to its variable ‘m’.

Moreover, we define the square function by taking the square of the variable. In the main body, we declare the array. We take 12 even numbers in the array. The data type of the input array and the resulting array is similar. The output is saved in a new array. Subsequently, we apply the transform() function. This function iterators to the start and end positions of a series. The series utilizes (array, array 12), which includes all components of the input array. It also contains the component that the array points to.

Here the resultant array is an output Iterator, and it iterates to the start of the series where the output is stored. The unary function takes a component of the category pointed to the array as an argument and then gave back a value that the resultant array can alter. The unary operation applies to the series defined in (array, array 12). This applies a method to every component of a series. At last, we use for loop. Every element is printed on a new line.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/2-17.jpg" data-lazy- height="275" src="data:image/svg xml,” width=”550″>

Binary Operation:

This technique carries out the binary operation on the components in the series (first element, last element) with the component present in the second position in the series. It saves the result in the series, starting with the result. The transform() applies a function that takes two components and receives two parameters from the input series for every pair of components. In this program, we execute binary operations on the specified data. If there is a need to add components from two arrays, we utilize the binary operator method.

#include


 

#include


 

#include


   

using namespace std;

int operator_increment (int k, int l) {

  return k l;

}

int main () {

  int a = 8;    

  int inp_arr1[] = {4, 7, 12, 36, 75, 23, 48, 69};

  int inp_arr2[] = {2, 15, 6, 95, 8, 73, 4, 80};

  int otp_arr[a];

  std::cout <<“First array:”;

  for(int k=0; k<a; k ){

    cout <<‘ ‘ <<inp_arr1[k];

  }

  cout <<n;

  std::cout <<“Second array:”;

  for(int k=0; k<a; k ){

    cout <<‘ ‘ <<inp_arr2[k];

  }

  cout <<n;

 


  std::transform (inp_arr1, inp_arr1 a, inp_arr2, otp_arr, operator_increment);

  std::cout <<“Resultant array:”;

  for(int k=0; k<8; k ){

    cout <<‘ ‘ << otp_arr[k];

  }

  cout <<n;

  return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/3-15.jpg" data-lazy- height="532" src="data:image/svg xml,” width=”690″>

In this instance, we integrate a new header file which is used for std::transform and another header file #include is used for std::vector. Now we apply the binary function by the transform function. We declare two variables ‘k’ and ‘l’ here, and it returns the addition of the ‘k’ variable into the ‘l’ variable. Further, we define two different arrays. There are 8 elements in each array. And to represent this, we use the variable ‘a’.

The data type of the arrays is an integer. The output is stored in a new array which is represented by ‘otp_arr’. We want to print the text ‘first array’; hence we use the cout function. We applied for loop for the first entered array. By this, we get the elements of the first array. We apply the newline character ‘n’.

Next, we again use a for loop to get the second array. The main portion of the program is to apply the transform function for these arrays. This function takes the first and last element of the first array, the first element of the second array, and the first element of the resultant array. Then the binary function is applied to two arrays. The std::transform() method applies a binary operator to every component in the range and saves the return value. In the same way, we get the resultant array. We also applied for loop for the resultant array.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/4-14.jpg" data-lazy- height="133" src="data:image/svg xml,” width=”581″>

This function also gets the beginning of the output array and a pointer to the binary function, which applies to the two defined arrays. The binary function accepts two components (one from each of the two series, respectively) as arguments. And it returns an array that is transformed to the data type of otp_arr.

Addition of two vectors:

We can use the std::transform function to make the target series similar to the input series and perform an in-place alteration. The succeeding example demonstrates how to utilize binary translations.

#include

#include


 

#include


 

#include  


 

int op_increase (int j) { return j; }

int main () {

  std::vector<int>abc;

  std::vector<int>xyz;

  for (int j=1; j<8; j )

    abc.push_back (j*20);  


                     

  xyz.resize(abc.size());  


                 

  std::transform (abc.begin(), abc.end(), xyz.begin(), op_increase);

                                                 


  std::transform (abc.begin(), abc.end(), xyz.begin(), abc.begin(), std::plus<int>());

                                                 


  std::cout <<“Output”;

  for (std::vector<int>::iterator it=abc.begin(); it!=abc.end(); it)

    std::cout <<‘ ‘ <<*it;

  std::cout <<n;

  return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/5-13.jpg" data-lazy- height="416" src="data:image/svg xml,” width=”680″>

When the program starts, first the libraries will be integrated. #include library is applied to the std::transform function. #include applies to the std::vector function. #include relates std::plus method. The function op_increase() is applied for conversions to increment the valuation of the parameters.

We integrate two vectors, and their data types are similar. After declaring for loop, we apply the std::transform function. We declare 8 elements in for loop. In the transform() function the series utilized is (abc.begin, abc.end), which includes all components between abc.begin and abc.end. Here xyz.begin iterates to the start location of the second series. Std::plus() is a built-in function and is utilized in a binary conversion to add two vectors. The outcome of the above-mentioned code looks like this:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/6-13.jpg" data-lazy- height="108" src="data:image/svg xml,” width=”592″>

Conclusion:

This article has explained the std::transform function. The function is utilized in two ways. A unary function takes only one component as an argument and gives back a value. A binary function that takes two components (one from every one of the two series) as arguments, and then it returns a resultant value. The binary operation always applies to couples of components in two ranges. It relates the specified function to a series and saves the output in another series.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/omar-150×150.png621ecdae40bd2.jpg" height="112" src="data:image/svg xml,” width=”112″>

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.