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
- Example 1 Sum
x + y
- Example 2 concatenate two strings
- Example 3 Concatenate different types. Note that in this case we do not inform the types for it.
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( 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:
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: