void myfun1(int& i){std::cout << i << " ";}void myfun2(int i, const char* prefix){std::cout << prefix << i << std::endl;}struct mystruct1 {void operator() (int& i){std::cout << i << " ";}} myobject1;struct mystruct2 {const char* prefix_;mystruct2(const char* prefix): prefix_(prefix) {}void operator() (int& i) {std::cout << prefix_ << i << std::endl;} };class myclass1{public:void operator() (int& i){std::cout << i << " ";}};class myclass2{public:const char* prefix_;myclass2(const char* prefix): prefix_(prefix){};void operator() (int& i){std::cout << i << " ";}};void test_for_each(){int ar[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};std::vector<int> v(ar, ar + 9);// 一個參數的普通函數std::cout << "myfunction1 output below: " << std::endl;for_each(v.begin(), v.end(), myfun1);std::cout << std::endl;// 二個參數的普通函數 std::cout << "myfunction2 output below: " << std::endl;for_each(v.begin(), v.end(), std::bind2nd(std::ptr_fun(myfun2), "i = "));// 不帶參的結構體std::cout << "mystruct1 output below: " << std::endl;for_each(v.begin(), v.end(), mystruct1());// for_each(v.begin(), v.end(), myobject1);// 這個方法也行.std::cout << std::endl;// 帶參的結構體std::cout << "mystruct2 output below: " << std::endl;for_each(v.begin(), v.end(), mystruct2("i = "));// 不帶參的類std::cout << "myclass1 output below: " << std::endl;for_each(v.begin(), v.end(), myclass1());std::cout << std::endl;// 帶參的類std::cout << "myclass2 output below: " << std::endl;for_each(v.begin(), v.end(), myclass2("i = "));std::cout << std::endl;}