适配器模式(c++)
#include<iostream> #include<vector> #include<algorithm> using namespace std; struct MyPrint { void operator()(int v1,int v2){ cout << v1 + v2 << endl; } }; //定义目标接口 我是配偶 适配成什么样 class Target { public: virtual void operator()(int v) = 0; }; //适配器 //class Adapter :public Target { //public: // virtual void operator()(int v) { // print(v, 100); // } //private: // MyPrint print; //}; class Adapter :public Target { public: Adapter(int param) { this->param = param; } virtual void operator()(int v) { print(v, param); } private: MyPrint print; int param; }; int main() { vector<int> v; for (int i = 0; i < 10; i++) { v.push_back(i); } for_each(v.begin(),v.end(),Adapter(5)); cin.get(); return 0; }