Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
打电话
std::count_if(vec.begin(), vec.end(), std::bind2nd(std::ptr_fun(foo), 17))
工作正常
bool foo(int, int),
但我不能让它工作
bool foo(const int &, const int &)
有没有办法让它工作或者我必须编写自己的适配器函数?
第二个参数是一个数字,不能转换为const int &.
const int &
您可以使用boost::bind以下技巧:
boost::bind
std::count_if (vec.begin(), vec.end(), boost::bind (foo, _1, 17));
编辑:
在我的第一个回复中,是的,您不能使用变量代替数字。我认为问题在于bind2nd并且ptr_fun没有正确定义以在构建内部对象时作为引用的情况下取消引用该类型,因此请使用 boost 或编写自己的仿函数类。ptr_funOperation
bind2nd
ptr_fun
Operation