std::bind2nd
is from C++98/03 (specifically, pre-lambdas).
bin2nd(f, x)
assumes that f
is a function (or at least something that can be invoked like a function) and x
is a value that an be passed to that function as its second parameter. It creates something that acts like a function. When you call the function it creates, it's equivalent to calling f(something, x)
, so it binds
x to the second parameter of its input function.
Nowadays, you'd probably write this more like:
transform(std::begin(numbers), std::end(numbers),
remainders,
[](int x) { return x % 2; });
As for what transform does, it's pretty much like a loop, so this is roughly equivalent to:
for (int i=0; i<3; i++)
remainders[i] = numbers[i] % 2;
Overall, I think the original code is kind of half-baked. It looks to me like it was probably written when all this stuff was fairly new (or at least new to the author). If I were going to do this job overall, I'd probably "collapse" the operations together a bit:
std::transform(std::begin(numbers), std::end(numbers),
std::ostream_iterator<std::string>(std::cout, "\n"),
[](int i) { return std::to_string(i) + (i % 2 == 0 ? " is even" : " is odd"); });
The original doesn't seem to have any use for the remainders
array except to hold an intermediate value before printing out the result, so it's probably better not to create/populate it at all.