Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Answer of exercise #9: No for-loops #0

 

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

 

 

 

 

 

Question

 

Replace the for-loop. You will need the following:

 

#include <vector>

void Triple(std::vector<int>& v)
{
  const int sz = v.size();
  for (int i=0; i!=sz; ++i)
  {
    v[i]*=3;
  }
}

 

 

 

 

 

Answer

 

#include <vector>
#include <algorithm
#include <numeric>
 
void Triple(std::vector<int>& v)
{
  std::transform(v.begin(),v.end(),v.begin(),
    std::bind2nd(std::multiplies<int>(),3));
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict