如何使用binder2nd,bind2nd和bind1st? 更具体何时使用它们,是否有必要? 另外,我正在寻找一些例子。如何使用binder和bind2nd函子?
1
A
回答
5
他们从来没有,严格来说,必要,因为你总是可以定义自己的自定义函数对象;但他们非常方便正是为了避免必须在简单情况下定义自定义函子。例如,假设您想要计算std::vector<int>
中> 10
中的项目。你当然可以...代码:
std::count_if(v.begin(), v.end(), gt10())
定义后:
class gt10: std::unary_function<int, bool>
{
public:
result_type operator()(argument_type i)
{
return (result_type)(i > 10);
}
};
但考虑如何更方便的是代码,而不是:
std::count_if(v.begin(), v.end(), std::bind1st(std::less<int>(), 10))
无需任何辅助函子类需要定义!)
1
粘合剂是C++的方式做currying。顺便说一句,检查出Boost Bind库
相关问题
- 1. find_if与bind2nd和函子
- 2. 与bind2nd和binary_function
- 3. bind1st和bind2nd
- 4. 使用std :: bind2nd与引用
- 5. 使用std :: bind2nd与推力
- 6. 如何使用Binder在我的C#函数中执行动态绑定?
- 7. C++如何使用函子使用std :: mutex和std :: lock_guard?
- 8. 使用Binder进行表单验证
- 9. 如何做Spring Cloud Steam Binder SPI韧性?
- 10. 如何了解Android Binder递归
- 11. JavaBinder FAILED BINDER TRANSACTION
- 12. 使用gwt和ui-binder在悬停上更改图像
- 13. 如何在javascript中使用函子
- 14. Android,JUnit测试和失败的BINDER TRANSACTION
- 15. Binder到字符串
- 16. 使用MVC Model Binder,如何防止绑定内部复杂对象的属性?
- 17. Scala - 如何在非函数类型上使用函子?
- 18. 如何使用sum和count函数
- 19. 如何使用file_get_contents()函数和的preg_match()
- 20. 如何使用parMap和monadic函数?
- 21. 如何使用jCarousel和PHP函数?
- 22. 如何使用setInterval和构造函数?
- 23. 使用SWFUpload时未调用.NET MVC 2.0自定义模型Binder
- 24. 如何使用离子和android的websockets?
- 25. 如何使用with子句和union sqlserver
- 26. 如何使用LINQ和IN子句
- 27. 如何使用$ a和$ b在Perl子
- 28. 如何使用节点和离子
- 29. 是否有可能在asp.net mvc中使用“复杂”模型和Data Annotation Model Binder?
- 30. 如何使PostgreSQL函数成为原子?
对,我明白,但是这是怎么回事? bool IsOdd(int i){return((i%2)== 1); } int main(){ int mycount; vector myvector; (int i = 1; i <10; i ++)myvector.push_back(i); // myvector:1 2 3 4 5 6 7 8 9 mycount =(int)count_if(myvector.begin(),myvector.end(),IsOdd); cout <<“myvector包含”<< mycount <<“奇数值。\ n”; return 0; } 这是从:http://www.cplusplus.com/reference/algorithm/count_if/ 他们没有定义任何仿的对象,只是一个简单的功能 –
Tom
2009-09-21 16:49:34
对不起,我没有格式化代码,但是代码这里: http://www.cplusplus.com/reference/algorithm/count_if/ – Tom 2009-09-21 16:50:21
@Tom,是的,在简单的例子中,函子可以是一个函数,但是,你必须先定义它远离使用的地方) - 活页夹很方便,因为它们可以让你避免这种情况(从来没有必要_,正如我已经说过的:只是_convenient _! - )。 – 2009-09-21 17:04:42