bind1st和bind2nd
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
typedef struct Print{
void operator()(int a) {
cout <<a<<"--";
}
}myPrint;
// binary_function<参数1,参数2,返回值类型>
struct myPrint2:public binary_function<int,int,void>{
void operator()(int a, int b)const {
cout <<a*b<<"--";
}
};
void fun() {
vector<int>v1{1,2,3,4};
for_each(v1.begin(), v1.end(), myPrint());
cout <<endl;
for_each(v1.begin(), v1.end(), bind2nd(myPrint2(),10));
cout <<endl;
for_each(v1.begin(), v1.end(), bind1st(myPrint2(),10));
}
int main() {
fun();
return 0;
}
image.png
bind
#include <iostream>
#include <functional>
using namespace std;
int fun(int a, char c, float f)
{
cout << a << endl;
cout << c << endl;
cout << f << endl;
return a;
}
int main()
{
auto bindFunc3 = bind(fun, std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
bindFunc3(100.1, 30, 'C');
return 0;
}
image.png
网友评论