Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) GetAbs

 

GetAbs is a math code snippet to create a container with absoluted values of another container.

 

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

//From http://www.richelbilderbeek.nl/CppMakeAbs.htm
template <typename T> struct Abs
  : public std::unary_function<T,T>
{
  const T operator()(const T& x) const
    { return (x < static_cast<T>(0.0) ? -x : x); }
};

//From http://www.richelbilderbeek.nl/CppMakeAbs.htm
template <typename T>
void MakeAbs(std::vector<T>& v)
{
  std::transform(v.begin(),v.end(),v.begin(),Abs<T>());
}

//From http://www.richelbilderbeek.nl/CppGetAbs.htm
template <typename T>
const std::vector<T> GetAbs(
  const std::vector<T>& a)
{
  std::vector<T> v(a);
  MakeAbs(v);
  return v;
}

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict