首页 > 编程语言 > 详细

C++——仿函数

时间:2020-05-15 16:49:12      阅读:43      评论:0      收藏:0      [点我收藏+]

它是个类,不是个函数。

模仿函数的类:能像普通函数一样传入给定数量的参数,还能存储或者处理更多我们需要的有用信息

https://www.cnblogs.com/decade-dnbc66/p/5347088.html

 

class StringAppend{
    public:
        explicit StringAppend(const string& str) : ss(str){}

        void operator() (const string& str) const{
             cout<<str<< <<ss<<endl;
        }
    
    private:
        const string ss;  
};

StringAppend myFunc("is world");
myFunc("hello");
>>>hellois world

 

class ShorterThan {
    public:
        explicit ShorterThan(int maxLength) : length(maxLength) {}
        bool operator() (const string& str) const {
            return str.length() < length;
        }
    private:
        const int length;
};
count_if(myVector.begin(), myVector.end(), ShorterThan(length));//直接调用即可

 

C++——仿函数

原文:https://www.cnblogs.com/yrm1160029237/p/10222097.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!