18
我正在尝试将std::accumulate
与std::min
结合使用。像这样的东西(不会编译):是否有可能使用std :: accumulate和std :: min?
vector<int> V{2,1,3};
cout << accumulate(V.begin()+1, V.end(), V.front(), std::min<int>);
这可能吗? 有没有可能为std::min
写包装仿函数?
我知道,我可以用lambda表达式做到这一点:
vector<int> V{2,1,3};
cout << std::accumulate(
V.begin()+1, V.end(),
V.front(),
[](int a,int b){ return min(a,b);}
);
而且我知道有std::min_element
。我不是想找到最小元素,我需要将std::accumulate
与std::min
(或::min
)结合起来用于我的库,该库允许在C++中使用函数式编程(如表达式)。
你可以使用一个丑陋投过'(const int的&(*)(const int的&,const int的&))的std ::分钟'。 –
2012-07-24 08:07:25
我倾向于更喜欢lambda版本。 – moooeeeep 2012-07-24 08:07:51
@JesseGood:你没有'static_cast'? :\ – Mehrdad 2012-07-24 08:08:04