我想了解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綁定任何參數。 –