《C++标准库》
函數對象
使用bind時注意,占位符有自己的命名空間std::placeholders,如果不在程序開始處using?std::placeholders,那么就要寫成:
std::bind(std::logical_and<bool>(),
? ? ? ? ? ? ? std::bind(std::greater_equal<int>(),std::placeholders::_1,50),
? ? ? ? ? ? ? std::bind(std::less_equal<int>(),std::placeholders::_1,80));
上面的表現出單參判別式,等價于x>=50&&x<=80。
?bind調用全局函數:bind(myToupper,_1),其中myToupper:
char myToupper(char c) {... }?bind調用成員函數:bind(&Person::print,_1,"This is: ");相當于調用param1.print("This is: ");
?
函數對象是一個類,因此可作為template模板實參,而尋常函數無法這樣。
函數對象擁有內部狀態(類的私有成員)。
for_each()算法可以返回其參數中的函數對象:
MeanValue mv=for_each(coll.begin(),coll.end(),MeanValue());
?
?
轉載于:https://www.cnblogs.com/ph829/p/5664447.html
總結
- 上一篇: 解决:Unknown table eng
- 下一篇: 浅析HTTP协议