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

发布于 2024-12-23 04:54:39 字数 1669 浏览 9 评论 0原文

我想创建一个 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 或任何其他库或框架)并不是首选。

I want to create an std::vector object (or any other standard or custom container type) with elements of custom and arbitrary functions whose signatures are all the same.

It should be something like this:

// 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));
}

If possible, I don't want to define these set of functions explicitly as below:

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) { /* ... */ }
};

An implementation with some standard library tool (like using std::function) is OK. But, a non-standard way of doing this (like using Boost, QT or any other library or framework) is not preferred.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

触ぅ动初心 2024-12-30 04:54:39

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

It sounds like you want lambda functions. If your C++ compiler implements this part of the C++11 standard yet, you can use them directly. Otherwise you might be able to use Boost Phoenix or Boost Lambda.

软甜啾 2024-12-30 04:54:39

假设您的编译器足够现代,您可以使用 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);
});

Assuming your compiler is modern enough, you can use the new std::function type and anonymous (lambda) functions introduced in 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);
});
注定孤独终老 2024-12-30 04:54:39

您可以使用 std::function 和 lambda 来完成此操作:

#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"));
}

You can do this using std::function and 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"));
}
你的背包 2024-12-30 04:54:39

函数指针已经足够了,甚至不需要使用 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;
}

Function pointers are fairly enough, no need to use even 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文