(C++) Answer of exercise \#9: No for-loops \#14
February 23, 2018 · View on GitHub
(C++) Answer of exercise #9: No for-loops #14
This is the answer of Exercise #9: No for-loops.
Question #14: Make square
Replace the for-loop. You will need:
- std::transform
- your own std::unary_function
#include <vector> void MakeSquare(std::vector<int>& v) { const int sz = v.size(); for (int i=0; i!=sz; ++i) { v[i]*=v[i]; } }
Answer
#include <algorithm> #include <functional> #include <vector> template <class T> struct Square : public std::unary_function<T,T> { const T operator()(const T& x) const { return x*x; } }; void MakeSquare(std::vector<int>& v) { std::transform(v.begin(),v.end(),v.begin(),Square<int>()); }