cpp::daily - Dynamic Addition and Concatenation

std::plus and std::transform


cpp::daily - Dynamic Addition and Concatenation

std::plus

std::plus<data type or nothing> - it’s a Function object to perform the addition. The object class whose call returns the result of adding its two arguments (as returned by the + operator).

  • Example 1 Sum x + y
#include <iostream>

int main(){
  int x = 3, y = 6;
  std::cout << std::plus<int>{}( x, y ) << '\n'; // 9
  return 0;
}
  • Example 2 concatenate two strings
std::string hello = "Hello, ", world = "World!";
std::cout << std::plus<std::string>{}( hello, world ) << '\n';

  • Example 3 Concatenate different types. Note that in this case we do not inform the types for it.
std::string ter = "Terminal ";
char * roo = "Root";
std::cout << std::plus<>{}( ter, roo ) << '\n'; // Terminal Root

Ahhh! But is it just for adding or concatenating? No, it is used when you need to get dynamic results and usually in conjunction with std::transform.

std::transform

std::transform( input1, last_input1, input2, operation );

Applies the given function to a range and stores the result in another range, keeping the order of the original elements and starting from the first.

Suppose you need to add all the elements of array1 with array2 in the generic way you would:

int array1[] {1, 2, 3},
    array2[] {4, 5, 6},
    total_elem = sizeof(array1) / sizeof(array1[0]),
    x[ total_elem ];

for (int i = 0; i < total_elem; i++) {
x[i] = array1[i] + array2[i];
}

for( auto y : x ){
  std::cout << y << ' ';
} // 5 7 9

std::cout << '\n';

Now optimizing your code with std::transform would look like: The std::plus as we are inside the std::transform we must not inform the operator: {}

#include <iostream>
#include <algorithm> // std::transform

int main(){
  int array1[] {1, 2, 3},
      array2[] {4, 5, 6},
      total_elem = sizeof(array1) / sizeof(array1[0]),
      x[ total_elem ];

  for (int i = 0; i < total_elem; i++) {
  x[i] = array1[i] + array2[i]; 
  } 

  std::transform( array1, array1 + total_elem, array2, x, std::plus<int>() );

  for( auto y : x ){
    std::cout << y << ' ';
  } // 5 7 9

  std::cout << '\n';
  return 0;
}

Like a Boss!


cppdaily cpp


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles