类构造函数初始化与map第二个参数

时间:2014-10-31 13:50:14

标签: c++ map constructor initialization

我有一张类似

的地图
  typedef float(*function)(vector<int>);
  map < string, function> mymap{
        { "maximum", &maxValue },
        { "minimum", &minValue },
        {... , ...},
  };

并将我在地图中使用的函数定义为

   float maxValue(vector<int> v){
       auto cit = max_element(v.begin(), v.end());
       return *cit;
   }

以及我在上面的地图定义中使用的其他函数的类似函数定义。

我在.h文件中有一个带有两个私有数据变量的类作为“字符串值;”和“浮动聚合”

现在,我的问题是我想为这个类定义一个构造函数,并初始化对象的成员数据in.cpp文件,其值为

  // aggregates is my class name
 aggregates::aggregates(string val, const vector<int>& v): value(val), aggregate(.....)//here is my problem how can I initialize aggregate from the map that I mentioned above.

提前致谢

1 个答案:

答案 0 :(得分:0)

class aggregates
{
private:
    float aggregate;
    string value;
public:
    aggregates(const string & val, const vector<int> & v, const map <string, function> & mymap) : value(val)
    {
        aggregate = mymap.at(value)(v); // instead of mymap[value](v), due to const&
    }
};
相关问题