Skip to content

Latest commit

 

History

History
77 lines (43 loc) · 1.81 KB

CppExerciseNoForLoopsAnswer17.md

File metadata and controls

77 lines (43 loc) · 1.81 KB

 

 

 

 

 

 

This is the answer of Exercise #9: No for-loops.

 

 

 

 

 

Question #17: Halve

 

Replace the for-loop. You will need:

 


#include <vector> void Halve (std::vector<double>& v) {   const int sz = static_cast<int>(v.size());   for (int i=0; i!=sz; ++i)   {     v[i]/=2.0;   } }

 

 

 

 

 

Answer

 


#include <algorithm> #include <numeric> #include <vector> //From http://www.richelbilderbeek.nl/CppHalve.htm void Halve (std::vector<double>& v) {   std::transform(v.begin(),v.end(),v.begin(),     std::bind2nd(std::divides<double>(),2.0)); }