在转换中使用bind1st和bind2nd的问题
Problem using bind1st and bind2nd with transform
我认为c++ 0x绑定要好得多,但在我使用c++ 0x的bind:
之前,我想了解旧的bind1st和2st:struct AAA
{
int i;
};
struct BBB
{
int j;
};
// an adaptable functor.
struct ConvertFunctor : std::binary_function<const AAA&, int, BBB>
{
BBB operator()(const AAA& aaa, int x)
{
BBB b;
b.j = aaa.i * x;
return b;
}
};
BBB ConvertFunction(const AAA& aaa, int x)
{
BBB b;
b.j = aaa.i * x;
return b;
}
class BindTest
{
public:
void f()
{
std::vector<AAA> v;
AAA a;
a.i = 0;
v.push_back(a);
a.i = 1;
v.push_back(a);
a.i = 2;
v.push_back(a);
// It works.
std::transform(
v.begin(), v.end(),
std::back_inserter(m_bbb),
std::bind(ConvertFunction, std::placeholders::_1, 100));
// It works.
std::transform(
v.begin(), v.end(),
std::back_inserter(m_bbb),
std::bind(ConvertFunctor(), std::placeholders::_1, 100));
// It doesn't compile. Why? How do I fix this code to work?
std::transform(
v.begin(), v.end(),
std::back_inserter(m_bbb),
std::bind2nd(ConvertFunctor(), 100));
std::for_each(m_bbb.begin(), m_bbb.end(),
[](const BBB& x){ printf("%dn", x.j); });
}
private:
std::vector<BBB> m_bbb;
};
int _tmain(int argc, _TCHAR* argv[])
{
BindTest bt;
bt.f();
}
为什么不能编译第三个转换函数?我如何修复这个代码的工作?
变化
struct ConvertFunctor : std::binary_function<const AAA&, int, BBB>
{
BBB operator()(const AAA& aaa, int x)
{
:
struct ConvertFunctor : std::binary_function<AAA, int, BBB>
{
BBB operator()(const AAA& aaa, int x) const
{
不要问我为什么,我只看编译错误信息
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- 用户定义类的Bind2nd问题
- 在转换中使用bind1st和bind2nd的问题