我想創建一個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
)是可以的。但是,這樣做(如使用升壓,QT或任何其他文庫或框架)的非標準的方式不是優選的。
你甚至不需要在這裏使用'function'(除非你使用VC2010)。無捕獲的lambda優雅地降級到函數指針。然而,如果你想使用除函數指針以外的東西,總是使用'function'。 – 2011-12-27 19:15:26