带有函数指针的映射
map with function pointers
我得到了一个函数,它从给定的向量中返回一个最小值作为
float minValue(vector<int> v){
auto it = min_element(v.begin(), v.end());
return *it;
}
现在我有一张类似的地图
map<std::string, function*>
{
{"min", /* here I need to use the above function call*/},
//similarly for other requirements too
{""}
}
如何使用键来指向函数指针映射值?
您实际上是如何声明function
的?你的地图定义应该看起来像
map<std::string, float (*)(vector<int> v)> fnMap {
{"min", &minValue } ,
{"", NULL }
};
至于你的意见
typedef float(*function)(vector<int>);
map<std::string, function> fnMap {
{"min", &minValue } ,
{"", NULL }
};
function*
将产生一个指向函数指针的指针。
相关文章:
- 如何在 C# 中映射双 C 结构指针?
- 智能指针作为无序映射键,并通过引用进行比较
- C++:映射结构中的结构指针
- 如何创建对象函数指针C++映射?
- 整数键映射到头文件中的成员函数指针
- C++在一个映射中存储不同的指针类型(并处理销毁)
- 添加要映射的对象的唯一指针
- 通过指针或引用将映射传递给函数?
- 在映射中存储模板类函数指针
- c++:复制、删除和运算符=在原始指针映射中
- 将唯一指针插入std::映射
- 在循环(C++)中用新指针填充映射
- C++:如何初始化包含具有给定键的指针列表的映射
- 如何将带指针的结构插入到映射中
- 重构MFC消息映射以包括完全限定的成员函数指针
- C 正确的方法来初始化和处理映射指针
- 定义基于模板的映射指针,以便在验证范围时将 int 解析为枚举
- C++中的静态无序映射指针初始化
- 动态分配映射指针值
- 如何在c++中维护映射指针的向量