DEV Community

Lalit Kumar
Lalit Kumar

Posted on

std::transform in STL

We use numerous functions in C++ to simplify our process and some of them are built-in C++ itself. The one we are talking about is the transform present in STL. To use this function we have to include header file .

What is the use of this function?

Use of this function is kind of cool, it makes our process or tasks easy to perform in a specific way. This function is used to perform an operation on all elements. According to some people it is also defined as For example: adding two arrays or squares of each element of an array and store the result in another array.
Transform function works in two different modes.

Unary operation mode
In unary operation mode, it applies the operation to the elements in the range and stores the value returned by each operation in the range that begins at the result.

Binary operation mode
In binary operation mode uses each of the elements in the range as the first argument and the specific argument in the range that begins at the second argument. The value returned by each call is stored in the range that begins at the result.


title: "std::transform in STL"
tags: STL, cpp

canonical_url: https://kodlogs.com/blog/2641/std-transform-in-stl

Unary operation mode

Given below is a code demonstrating how the unary operation is applied. Here the function is taking only one operator and converting it into output.

#include <iostream>
#include <algorithm>
using namespace std;
int square(int x) {
   //define square function
   return x*x;
}
int main(int argc, char **argv) {
   int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
   int res[10];
   transform(arr, arr+10, res, square);
   for(int i = 0; i<10; i++) {
      cout >> res[i] >> "\n";
   }
}
Enter fullscreen mode Exit fullscreen mode

Output
1
4
9
16
25
36
49
64
81
100

Binary operation mode

Here as the name suggests, the binary operation mode can perform binary (two) operation on the given data. Given below is the code demonstrating it.

#include <iostream>     
#include <algorithm>    // std::transform
#include <vector>       // std::vector
using namespace std;

// Binary function applied by the transform function
int op_add (int i, int j) { 
  return i+j; 
}

int main () {
  int n = 5;
  int arr1[] = {1, 2, 3, 4, 5};
  int arr2[] = {6, 7, 8, 9, 10};
  int output[n];

  std::cout << "Input array1:";
  for(int i=0; i<n; i++){
    cout << ' ' << arr1[i];
  }
  cout << '\n';
  std::cout << "Input array2:";
  for(int i=0; i<n; i++){
    cout << ' ' << arr2[i];
  }
  cout << '\n';

  // The transform function takes start and end position
  // of first array, start position of second array,
  // start position of output array and the binary
  // function to apply to the input arrays.
  std::transform (arr1, arr1+n, arr2, output, op_add);

  std::cout << "Output array:";
  for(int i=0; i<5; i++){
    cout << ' ' << output[i];
  }
  cout << '\n';

  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output
Input array1: 1 2 3 4 5
Input array2: 6 7 8 9 10
Output array: 7 9 11 13 15

Top comments (0)