标准库算法for_each和boost库中Bind使用方法:
#include <boost/bind.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Shape
{
public:
virtual void draw()
{
cout<<"Shap::draw"<<endl;
}
};
class Circle : public Shape
{
public:
virtual void draw()
{
cout<<"Cicle::draw"<<endl;
}
};
int main(int argc, char* argv[])
{
Shape* a = new Shape();
Shape* b = new Circle();
vector<Shape*> d;
d.push_back(a);
d.push_back(b);
for_each(d.begin(), d.end(), mem_fun(&Shape::draw));//根据类型选择draw的类型。
for_each(d.begin(), d.end(), boost::bind(&Shape::draw, _1));
delete a;
delete b;
return 0;
}