我想创建一个映射,其中键是一个函数名作为字符串,值是函数本身.所以像这样......
#include <cmath>
#include <functional>
#include <map>
#include <string>
typedef std::function<double(double)> mathFunc;
int main() {
std::map< std::string, mathFunc > funcMap;
funcMap.insert( std::make_pair( "sqrt", std::sqrt ) );
double sqrt2 = (funcMap.at("sqrt"))(2.0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
将用于在某个输入值上调用sqrt函数.然后你当然可以在地图上添加其他函数,如sin,cos,tan,acos等,然后通过一些字符串输入调用它们.我的问题是地图中的值类型应该是什么,函数指针和std :: function在std :: make_pair行给出以下错误
error: no matching function for call to 'make_pair(const char [5], <unresolved overloaded function type>)'
Run Code Online (Sandbox Code Playgroud)
那么我的值类型应该是什么样的内置函数,如std :: sqrt?
谢谢
| 归档时间: |
|
| 查看次数: |
428 次 |
| 最近记录: |