如何将函数应用于数组中的所有元素(在C ++模板类中)

时间:2015-04-06 09:38:55

标签: c++ templates function-pointers

我有一个存储数字数组的模板类,我想将现有(标量)函数应用于每个元素。例如,如果我们假设我的类是std :: vector,那么我希望能够在所有元素上调用(例如)std :: cos函数。

也许一个电话看起来像这样:

std::vector<float> A(3, 0.1f);
std::vector<float> B = vector_function(std::cos, A);

N.B。我还必须处理std :: complex&lt;&gt;类型(为其调用适当的复杂std :: cos函数)。

我发现this answer建议将函数类型作为模板参数:

template<typename T, typename F>
std::vector<T> vector_function(F func, std::vector<T> x)

然而,我根本无法使用它(可能是因为像std :: sin和std :: cos这样的函数都是模板化和重载的?)。

我也尝试使用std::transform,但这很快变得非常难看。对于非复杂类型,我设法使用typedef:

std::vector<float> A(2, -1.23f);
typedef float (*func_ptr)(float);
std::transform(A.begin(), A.end(), A.begin(), (func_ptr) std::abs);

但是,使用std :: complex&lt;&gt;尝试相同的技巧类型导致运行时崩溃。

有没有一种很好的方法让这个工作?我已经坚持了很久。

3 个答案:

答案 0 :(得分:4)

我仍然认为你应该使用std::transform

template <class OutputIter, class UnaryFunction>
void apply_pointwise(OutputIter first, OutputIter last, UnaryFunction f)
{
    std::transform(first, last, first, f);
}

此函数不仅适用于std::vector类型,而且适用于具有begin()end()成员函数的任何容器,它甚至适用于C风格的数组,借助于免费功能std::beginstd::end。一元函数可以是任何自由函数,函子对象,lambda表达式,甚至是类的成员函数。

对于std::sin的问题,这个自由函数是模板化的,因此编译器无法知道您需要哪个模板实例化。

如果您有权访问C ++ 11,那么只需使用lambda表达式:

std::vector<float> v;
// ...
apply_pointwise(v.begin(), v.end(), [](const float f)
{
    return std::sin(f);
});

这样,编译器就知道它应该用T=float替换模板参数。

如果你可以使用C函数,你也可以使用函数sinf,它不是模板化的,并以float作为参数:

apply_pointwise(v.begin(), v.end(), sinf);

答案 1 :(得分:0)

你应该看看Richel Bilderbeek(Math code snippet to make all elements in a container positive)发表的这篇文章,它会告诉你为什么abs不会像这样在变换中起作用。它无法工作的原因是由于abs函数结构(见http://www.cplusplus.com/reference/cstdlib/abs/)。您将看到abs本身不是模板化的,与functional库中的其他函数(主要是二进制函数)不同。 Richel网站上提供的一个解决方案,向您展示如何应用abs来表示整数向量。

现在,如果您想使用transform将abs应用于容器,您应该知道转换函数接收到一个复杂的对象,并且不知道如何对它应用abs。解决这个问题的最简单方法是简单地编写自己的模板化一元函数。

下面有一个例子,我将abs应用于复杂对象的实部和虚部。

#include <iostream>
#include <vector>
#include <algorithm>
#include <complex>
#include <functional>

template <typename T>
std::complex<T> abs(const std::complex<T> &in) {
  return std::complex<T>(std::abs(in.real()), std::abs(in.imag()));
};

int main() {
  std::vector<std::complex<int>> v;
  std::complex<int> c(10,-6);
  v.push_back(c);
  std::complex<int> d(-5, 5);
  v.push_back(d);

  std::transform(v.begin(), v.end(), v.begin(), abs<int>);

  //Print out result
  for (auto it = v.begin(); it != v.end(); ++it)
    std::cout << *it << std::endl;

  return 0;
}

正如您所提到的,您想要从complex库中应用abs。为了避免未指定的行为(请参阅this),您将使用double类型名称来表示复杂对象。

#include <iostream>
#include <vector>
#include <algorithm>
#include <complex>
#include <functional>

int main() {
  std::vector<std::complex<double>> v;
  std::complex<double> c(3, 4);
  v.push_back(c);
  std::complex<double> d(-5, 5);
  v.push_back(d);

  std::transform(v.begin(), v.end(), v.begin(), std::abs<double>); //abs from <complex>

  //Print out result
  for (auto it = v.begin(); it != v.end(); ++it)
    std::cout << *it << std::endl;

  return 0;
}

答案 2 :(得分:0)

如果您可以使用valarray而不是vector,那么您可以这样做

#include <iostream>
#include <valarray>

int main()
{
valarray<double> dva{0.2, 0.4, 0.6, 0.8,1.0};

// scalar operation   
dva += 0.25; // add 0.25 to each element of valarray inplace

// applying trignometric function
// apply sin function to each element and returns new valarray   
valarray<double> dva2 = std::sin(0.7 * dva);

// apply custom function to each 
// element. returns new valarray   
valarray<double> dva3 = dva.apply([](double dval) {
return (dva * dva)/2;
});

return 0;
}