當(dāng)前位置:
首頁 >
《C++标准库》
發(fā)布時間:2025/7/14
39
豆豆
函數(shù)對象
使用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));
上面的表現(xiàn)出單參判別式,等價于x>=50&&x<=80。
?bind調(diào)用全局函數(shù):bind(myToupper,_1),其中myToupper:
char myToupper(char c) {... }?bind調(diào)用成員函數(shù):bind(&Person::print,_1,"This is: ");相當(dāng)于調(diào)用param1.print("This is: ");
?
函數(shù)對象是一個類,因此可作為template模板實參,而尋常函數(shù)無法這樣。
函數(shù)對象擁有內(nèi)部狀態(tài)(類的私有成員)。
for_each()算法可以返回其參數(shù)中的函數(shù)對象:
MeanValue mv=for_each(coll.begin(),coll.end(),MeanValue());
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/ph829/p/5664447.html
總結(jié)
- 上一篇: 解决:Unknown table eng
- 下一篇: 理解MVC模式