【问题标题】:map with function pointers [closed]带有函数指针的映射[关闭]
【发布时间】:2014-12-27 07:33:08
【问题描述】:

我得到了一个函数,它从给定向量中返回最小值为

    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
  {""}
 }

如何使用键指向函数指针映射值?

【问题讨论】:

    标签: c++ map function-pointers


    【解决方案1】:

    您实际上是如何声明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* 将产生一个指向函数指针的指针。

    【讨论】:

    • 对不起,我忘了提。我使用了像 typedef float(*function)(vector); 这样的 typedef
    • @user3979554 我在回答中采用了这个。
    • 但是当我这样做时会抛出错误 c2440: cannot convert from initializer-list to map, 我该如何解决这个问题?
    • 开启-std=c++11
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 2021-02-27
    • 1970-01-01
    • 2017-04-26
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多