如何在不明确定义函数的情况下创建函数的std :: vector?

问题描述 投票:4回答:4

我想创建一个std :: vector对象(或任何其他标准或自定义容器类型),其中包含自定义和任意函数的元素,这些函数的签名都是相同的。

它应该是这样的:

// Define the functions and push them into a vector
std::vector<????> MyFunctions;
MyFunctions.push_back(double(int n, float f){ return (double) f / (double) n; });
MyFunctions.push_back(double(int n, float f){ return (double) sqrt((double) f) / (double) n; });
// ...
MyFunctions.push_back(double(int n, float f){ return (double) (f * f) / (double) (n + 1); });

// Create an argument list
std::vector<std::pair<int, float>> ArgumentList;
// ...

// Evaluate the functions with the given arguments
// Suppose that it is guarantied that ArgumentList and MyFunctions are in the same size
std::vector<double> Results;
for (size_t i=0; i<MyFunctions.size(); i++)
{
    Results.push_back(MyFunctions.at(i)(ArgumentList.at(i).first, ArgumentList.at(i).second));
}

如果可能,我不想明确定义这些函数集,如下所示:

class MyClass
{
    public:
        void LoadFunctions()
        {
            std::vector<????> MyFunctions;
            MyFunctions.push_back(MyFoo_00);
            MyFunctions.push_back(MyFoo_01);
            MyFunctions.push_back(MyFoo_02);
            // ...
            MyFunctions.push_back(MyFoo_nn);
        }

    private:
        double MyFoo_00(int n, float f) { /* ... */ }
        double MyFoo_01(int n, float f) { /* ... */ }
        double MyFoo_02(int n, float f) { /* ... */ }
        // ...
        double MyFoo_nn(int n, float f) { /* ... */ }
};

使用一些标准库工具(如使用std::function)的实现是可以的。但是,这样做的非标准方式(如使用Boost,QT或任何其他库或框架)不是首选。

c++ vector functional-programming function-pointers function-call
4个回答
3
投票

假设您的编译器足够现代,您可以使用C ++ 11中引入的新的std::function类型和匿名(lambda)函数:

std::vector<std::function<double(int, float)>> MyFunctions;
MyFunctions.push_back([](int n, float f) {
    return (double) f / (double) n;
});
MyFunctions.push_back([](int n, float f) {
    return (double) sqrt((double) f) / (double) n;
});
// ...
MyFunctions.push_back([](int n, float f) {
    return (double) (f * f) / (double) (n + 1);
});

6
投票

听起来你想要lambda functions。如果您的C ++编译器实现了C ++ 11标准的这一部分,您可以直接使用它们。否则你可以使用Boost PhoenixBoost Lambda


2
投票

你可以使用std::function和lambdas来做到这一点:

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

struct dispatcher {
  template <typename F, typename Pair>
  double operator()(const F& func, const Pair& p) const {
    return func(p.first, p.second);
  }
};

int main() {
  std::vector<std::function<double(int,double)>> functions;
  functions.push_back([](int n, float f) { return double(f)/double(n); });

  std::vector<std::pair<int, float>> args = {std::make_pair(1, 10.0f)};

  std::vector<double> results;

  std::transform(functions.begin(), functions.end(), args.begin(), std::back_inserter(results), dispatcher());

  std::copy(results.begin(), results.end(), std::ostream_iterator<double>(std::cout, "\n"));
}

0
投票

函数指针足够了,甚至不需要使用std :: function:

#include<iostream>
#include<vector>
#include<cmath>

int main()
{
      std::vector<double (*)(double)> vec;
      vec.push_back([](double x) {return cos(x);});
      vec.push_back([](double x) {return sin(x);});
      vec.push_back([](double x) {return tan(x);});
      for (auto f: vec)
          std::cout<<f(M_PI/4)<<'\n';
      return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.