#include #include #include #include using namespace std; class TakeCos{ public: void operator()(double& x){ x=cos(x); } // function object }; void doubleout(double& x) { cout<< x<<" ";} void vector_out(vector & x){ for_each (x.begin(), x.end(), doubleout); cout< x; x.push_back(1.1); x.push_back(2.2); x.push_back(3.3); cout << "vector x : "; vector_out(x); TakeCos Cos ; // create one instance of TakeCos, same time one function object for_each(x.begin(), x.end(), Cos); cout << "vector cos(x) : "; vector_out(x); return 0; }