#include #include #include using namespace std; // this is a functor struct add_x { add_x(int x) : x(x) {} int operator()(int y) const { return x + y; } private: int x; }; ostream &operator <<(ostream &out,const vector MyVector) { for(int idx=0;idx vals (myints, myints + sizeof(myints) / sizeof(int) ); vector out(vals.size()); cout << vals << endl; // Pass a functor to std::transform, which calls the functor on every element // in the input sequence, and stores the result to the output sequence transform(vals.begin(), vals.end(), vals.begin(), add_x(1)); cout << vals << endl; transform(vals.begin(), vals.end(), vals.begin(), add_x(5)); cout << vals << endl; transform(vals.begin(), vals.end(), vals.begin(), add_x(-11)); cout << vals << endl; transform(vals.begin(), vals.end(), vals.begin(), add42); cout << vals << endl; cout << "Value to add to all elements >"; int value; cin >> value; // Create a functor to add that value add_x add_val(value); transform(vals.begin(), vals.end(), vals.begin(), add_val); cout << vals << endl; // Now pass a constructed functor each time transform(vals.begin(), vals.end(), vals.begin(), add_x(value*2)); cout << vals << endl; // Store the value in a different vector, using an iterator to it. transform(vals.begin(), vals.end(), out.begin(), add_x(value*2)); cout << out << endl; }