如何在不显式定义函数的情况下创建函数的std::vector ?
How do I create an std::vector of functions without defining the functions explicitly?
我想创建一个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或任何其他库或框架)。
听起来你想要lambda函数。如果您的c++编译器实现了c++ 11标准的这一部分,您可以直接使用它们。或者你可以使用Boost Phoenix或者Boost Lambda
假设您的编译器足够现代,您可以使用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);
});
您可以使用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"));
}
函数指针已经足够了,甚至不需要使用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;
}
相关文章:
- 无法引用该函数" std:: variant <_Types...> operator =",因为此函数已被删除
- 错误:使用已删除的函数"std::unique_ptr<...>
- 我有模板功能.通过使用函数std::for_each,用这个容器中的最大数字替换每个正数
- 如何使用函数std::min()来计算最小值
- 删除了 g++ 在 linux 中basic_stringstream函数 std
- 如何在 c++ 中使用返回类型导出函数 std::map
- 错误:使用已删除的函数"std::atomic<_Tp>::atomic() [_Tp = node]"
- 使用 SFINAE 检查函数 std::to_string 是否存在类型
- C++14 unique_ptr并使用已删除的函数'std::unique-ptr' unique_ptr错误
- 为什么函数 std::basic_streambuf::setg() 需要非常量 agruments
- C++将已删除的函数 std::unique_ptr 与基类一起使用
- 奇怪的错误:当没有真正创建指针时,使用已删除的函数'std::unique_ptr<_Tp,_Dp>::unique_ptr
- 通过模板构造函数(std ::功能和迷你案例)将构造函数触发
- 错误:使用已删除的函数"std::atomic<_Tp>::atomic()
- 错误:使用已删除的函数std::basic_ofstream(OpenCV和C++11)
- 类成员容器迭代器在成员函数std::find中不兼容,但为什么不兼容呢
- 没有构造函数 std::istream_iterator 的实例
- 错误:没有与参数列表匹配的重载函数"std::make_shared"实例
- 错误:使用已删除的函数"std::thread::thread(const std::thread&)"
- 不能在没有对象的情况下调用成员函数 std::string class::function()