#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct MyPrint{
public:
void operator()(int v1, int v2){
cout << v1 + v2 << endl;
}
}
// 定义目标接口
class Target{
public:
virtual void operator()(int v) = 0;
}
//写适配器
class Adapter : public Target{
public:
Adapter(int param){
this->param = param;
}
virtual void operator()(int v){
print(v, param);
}
public:
MyPrint print;
int param;
}
Adapter MyBind2nd(int v){
return Adapter(v);
}
int main(){
vector<int> v;
for(int i = 0; i < 10; i++) {
v.push_back(i);
}
for_each(v.begin(), v.end(), Mybind2nd(10));
system("pause");
return 0;
}
适配器模式 c++ 学习记录
最新推荐文章于 2024-11-17 15:19:52 发布