I was reading C++ reference and came across std::plus function with an example. Which is pretty straight forward, its simply adds lhs and rhs. The code was:
#include <functional>
#include <iostream>
int main()
{
std::string a = "Hello ";
const char* b = "world";
std::cout << std::plus<>{}(a, b) << '\n';
}
output: Hello world
I changed it to
#include <functional>
#include <iostream>
int main()
{
int a = 5;
int b = 1;
std::cout << std::plus<int>{}(a, b) << '\n';
}
output : 6
Now I made
foo vector = 10 20 30 40 50
bar vector = 11 21 31 41 51
I called:
std::transform (foo.begin(), foo.end(), bar.begin(), foo.begin(), std::plus<int>());
and it gave 21 41 61 81 101 which I understand it is adding up both foo and bar. But how it was passed to std::plus function?
std::transform () in C++ STL (Perform an operation on all elements) Consider the problem of adding contents of two arrays into a third array. It is given that all arrays are of same size. Following is simple C++ program without transform ().
std::plus<> is a functor, which is just fancy talk for a class that implements operator (). Here's an example: struct plus { template <typename A, typename B> auto operator () (const A& a, const B& b) const { return a + b; } }; The std::transform you have there is roughly equivalent to:
std::plus specifically is a binary operator, so it takes two arguments. std::Transform will send it elements from the two vectors. You can also use std::bind to turn binary operators into one argument operators, basically binding a constant to the second argument.
The standard library provides a specialization of std::plus when T is not specified, which leaves the parameter types and return type to be deduced.
std::plus<>
is a functor, which is just fancy talk for a class that implements operator()
. Here's an example:
struct plus {
template <typename A, typename B>
auto operator()(const A& a, const B& b) const { return a + b; }
};
The std::transform
you have there is roughly equivalent to:
template<typename InputIt1, typename InputIt2,
typename OutputIt, typename BinaryOperation>
OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2,
OutputIt d_first, BinaryOperation binary_op)
{
while (first1 != last1) {
*d_first++ = binary_op(*first1++, *first2++);
}
return d_first;
}
Here, binary_op
is the name given to std::plus<>
. Since std::plus<>
is a functor, C++ will interpret the "call" to it as a call to the operator()
function, giving us our desired behavior.
std::plus one of a set of functors provided by the STL. If your familiar with functional programming, they are a convenience for function composition. std::plus specifically is a binary operator, so it takes two arguments. std::Transform will send it elements from the two vectors. You can also use std::bind to turn binary operators into one argument operators, basically binding a constant to the second argument.
Personally I think std::plus and its like were more useful before the advent of c++11 lambda functions. Lambda's allow you to define a set of operations to perform on your data without having to deal with binds or std::plus and its ilk.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With