#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class Myprint : public binary_function<int, int, void> {
public:
Myprint(int v) : m_v(v) {}
void operator() (int v1) const {
//cout << "v1 = " << v1 << " v2 = " << v2 << endl;
cout << v1 + m_v << endl;
}
public:
int m_v;
};
void FunMyPrint(int val) {
cout << val << endl;
}
void test01() {
//Myprint print;
vector<int> vv;
for (int i = 0; i < 10; i++) {
vv.push_back(i);
}
int num = 200;
for_each(vv.begin(), vv.end(), Myprint(100));
//Myprint(100)等同Myprint(100).operator()(10);
//Myprint(100)等同于bind1st bind2nd带参数创建对象
//绑定适配器,将一个二元函数对象转变成一元函数对象
//仿函数适配器 bind1st bind2nd 绑定适配器
}
int main() {
test01();
getchar();
return 0;
}
改造自己的构造函数,达到等同,仿函数适配器 bind1st bind2nd
于 2023-05-25 10:52:54 首次发布