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)を使用した実装は問題ありません。ただし、これを行うための非標準的な方法(BoostQT、またはその他のライブラリやフレームワークを使用するなど)は推奨されません。

4

4 に答える 4

6

ラムダ関数が必要なようです。C++コンパイラがC++11標準のこの部分をまだ実装している場合は、それらを直接使用できます。それ以外の場合は、 BoostPhoenixまたはBoostLambdaを使用できる可能性があります。

于 2011-12-27T19:09:45.813 に答える
3

std::functionコンパイラが十分に最新であると仮定すると、C ++ 11で導入された新しい型と無名(ラムダ)関数を使用できます。

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);
});
于 2011-12-27T19:13:32.987 に答える
2

ラムダを使用してこれを行うことができますstd::function

#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"));
}
于 2011-12-27T19:16:09.803 に答える
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;
}
于 2019-03-20T17:52:17.967 に答える