【发布时间】:2014-10-31 13:50:13
【问题描述】:
我有一张类似的地图
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 文件中有一个包含两个私有数据变量的类作为“字符串值;”和“浮动聚合”
现在,我的问题是我想为这个类定义一个构造函数,并在.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.
提前致谢
【问题讨论】:
-
您确定要在每次尝试计算最大值时复制向量吗?
-
这看起来不像是一个问题,更像是“为我做作业”
-
如果您使用的是 C++11,您可以使用
std::function代替函数指针的 typedef。 -
@JBL:我假设是 C++11 之前的版本,因为那里的初始化列表部分是微不足道的。
-
我会将向量作为常量引用传递。
标签: c++ map constructor initialization