我有以下方法:
CalculateThreshold(std::vector<double> &data, int thres)
{
// include functionality
}
然后我有一个 vector 的 vector ,我想使用 std::for_each
迭代它,我尝试使用以下内容:
std::for_each(
std::begin(results),
std::end(results),
std::bind2nd(std::mem_fun(&CalculateThres), 1));
但我不断收到错误消息:
note: candiditates are template std::mem_fun_t<_Ret, _Tp> std::mem_fun(_Ret (_Tp::*)())
有人对我哪里出错有建议吗?对我来说,这应该可行!
最佳答案
只需使用 std::bind
,不需要 std::mem_fun
(如评论中所述)。
std::for_each(
std::begin(results),
std::end(results),
std::bind(CalculateThres, std::placeholders::_1, 1));
此外,bind1st
和 bind2nd
已弃用,您应该使用 bind
反而。同样,mem_fun
也已弃用,其替换为 mem_fn
.
关于C++11 for_each 传递多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23573934/