【发布时间】:2010-07-22 15:20:39
【问题描述】:
我有一个函数A,它接受一个谓词函数作为它的参数。
我有另一个函数B,它接受char 并返回int,还有一个函数C 接受int 并返回bool。
我的问题是如何绑定B 和C 以将其传递给函数A。
类似:
A(bindfunc(B,C))
我知道boost::bind 有效,但我正在寻找 STL 解决方案。
例如,
int count(vector<int> a, pred func); // A
//this functions counts all elements which satisfy a condition
int lastdigit(int x); // B
//this function outputs last digit(in decimal notation) of number x
bool isodd(int x); // C
//this function tells if number x is odd
// i want to find the count of all such numbers in a vector whose last digit is odd
// so i want something like
count(vector<int> a, bind(lastdigit, isodd))
一种不好的方法是创建一个冗余函数D,它显式执行绑定操作。
【问题讨论】:
-
也许一个更具体的例子会有所帮助......
-
为什么选择社区维基? (顺便说一句,为清楚起见,请显示 A、B 和 C 的函数声明。)
-
int count(vector
a, pred func);//这个函数统计所有满足条件的元素 int lastdigit(int x);//这个函数输出最后一位数字(十进制) of number x bool isodd(int x);//这个函数告诉如果数字 x 是奇数我想找到一个向量中所有这些数字的计数,它的最后一位数字是奇数所以我想要类似 count(vector a, bind(lastdigit,isodd)) 如果我不清楚请告诉我 -
@KennyTM:这样每个人都可以在某个地方出现问题时进行编辑,从而增强它,并最终保持一切就绪
标签: c++ function stl binding predicate