我想了解std :: bind如何工作。我写了以下内容:在C++中使用std :: bind与二进制操作函数
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std::placeholders;
int fun2(int i,int j)
{
return i+j;
}
int fun(int i)
{
return i;
}
int main()
{
std::vector<int> v9={1,2,3,4,5,6,6,7};
std::transform(v9.begin(),v9.end(),v9.begin(),[](int i){return i;}); //works
std::transform(v9.begin(),v9.end(),v9.begin(),fun); //works
std::transform(v9.begin(),v9.end(),v9.begin(),std::bind(fun,_1)); //works
std::transform(v9.begin(),v9.end(),v9.begin(),std::bind(fun2,_1,_2)); //does not work
}
std :: transform还接受二进制操作函数。所以我试图写fun2并使用std :: bind(main的最后一行),但它不起作用。有人可以给我任何例子如何使用std :: bind占位符(2,3或更多)?
'std :: transform'只传递一个参数。你的意思是做'std :: bind(fun2,_1,5)'吗? – chris
除了当它通过两个:[(2)](http://en.cppreference.com/w/cpp/algorithm/transform :) :) – jrok
如果你不'使用'std :: bind'确实没有意义, t绑定任何参数。 –