/* Exercise 4.3.1: Fill two containers with the same number of integer values. Create a new container, whose elements are the sum of the appropriate elements in the original container. Hint: The library provides an algorithm and a function object to do the exercise. */ #include #include #include #include #include using namespace std; // Value generator simply doubles the current value // and returns it template class generate_val { private: T val_; public: generate_val(const T& val) : val_(val) {} T& operator()() { val_ += val_; return val_; } }; typedef ostream_iterator IntOsIt; void main() { generate_val gen(1); vector v1(10),v2(10),v3(10); generate(v1.begin(), v1.end(), gen); v2=v1; transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),plus() ); IntOsIt os(cout," "); copy(v3.begin(), v3.end(), os); cout<<"\nPress any key to exit..."; while(!cin.get()) ; };