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

January 10, 2021 ยท View on GitHub

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

Question #13: MakeAbs

Replace the for-loop. You will need:

#include <cmath>
#include <vector>
 
void MakeAbs(std::vector<int>& v)
{
  const int sz = v.size();
  for (int i=0; i!=sz; ++i)
  {
    v[i] = std::abs(v[i]);
  }
}

Answer

#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>

void MakeAbs(std::vector<int>& v)
{
  std::transform(
    v.begin(), 
    v.end(), 
    v.begin(), 
    std::ptr_fun<int,int>(std::abs)
  );
}

Much thanks to Andrei Kuzmenko for pointing out that std::abs is an overloaded function, not a template function, due to which std::transform() cannot determine the required variant of std::abs() without the programmer's help.