【发布时间】:2018-07-13 17:00:24
【问题描述】:
我遇到了这段代码。从输出中我可以推断出余数数组在除以 2 时存储数字数组的余数。但语法对我来说并不熟悉。
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
int main ( )
{
int numbers[ ] = {1, 2, 3};
int remainders[3];
transform ( numbers, numbers + 3, remainders, bind2nd(modulus<int>( ), 2) );
for (int i = 0; i < 3; i++)
{
cout << (remainders[i] == 1 ? "odd" : "even") << "\n";
}
return 0;
}
transform 和 bind2nd 在这种情况下做了什么?我阅读了文档,但我不清楚。
【问题讨论】:
-
std::transform,不知道 :bind2nd()究竟是什么。 -
@πάνταῥεῖ
std::bind2nd。在 C++11 中已弃用并在 C++17 中删除。 -
@Miles Cool,可能会解释很多为什么 OP 会询问。
-
那么可能会有类似
bind2Tuple()的东西作为替代品。 -
是的,它已被
std::bind和 lambdas 取代。