【发布时间】:2020-07-05 06:25:39
【问题描述】:
我目前正在尝试将函数绑定到我打算使用的算法。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
vector<int> coll{1, 2, 3, 4, 5, 6};
vector<int>::iterator pos;
pos = find_if (coll.begin(), coll.end(),
bind(greater<int>(),_1,3));
return 0;
}
并获得此错误反馈:
AlgoTesting.cpp: In function ‘int main()’:
AlgoTesting.cpp:184:41: error: ‘_1’ was not declared in this scope
bind(greater<int>(),_1,3)); // criterion
^~
AlgoTesting.cpp:184:41: note: suggested alternative:
In file included from algostuff.hpp:15:0,
from AlgoTesting.cpp:5:
/usr/include/c++/7/functional:275:34: note: ‘std::placeholders::_1’
extern const _Placeholder<1> _1;
^~
我很难理解错误日志的含义。有人知道我在这里缺少什么吗?
【问题讨论】:
-
使用 lambda 而不是
std::bind通常更容易,提供更好的错误消息,并且作为奖励甚至可能产生更快的代码 -
@AlanBirtles:谢谢,毫无疑问。还请在下面找到我对答案的评论。由于
bind本身的使用,我遇到了这个问题,这就是我写这篇文章的原因。这真的不是关于哪个更好。