如何在不显式定义函数的情况下创建函数的 std::vector ?
我想创建一个 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 或任何其他库或框架)并不是首选。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来您想要 lambda 函数。如果您的 C++ 编译器尚未实现 C++11 标准的这一部分,您可以直接使用它们。否则,您也许可以使用 Boost Phoenix 或 Boost 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.
假设您的编译器足够现代,您可以使用 C++11 中引入的新
std::function
类型和匿名 (lambda) 函数:Assuming your compiler is modern enough, you can use the new
std::function
type and anonymous (lambda) functions introduced in C++11:您可以使用 std::function 和 lambda 来完成此操作:
You can do this using
std::function
and lambdas:函数指针已经足够了,甚至不需要使用 std::function:
Function pointers are fairly enough, no need to use even std::function: