zoukankan      html  css  js  c++  java
  • C++ STL 之 函数对象

    重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象,也叫仿函数(functor),其实就是重载“()”操作符,使得类对象可以像函数那样调用。
    注意:1.函数对象(仿函数)是一个类,不是一个函数。2.函数对象(仿函数)重载了”() ”操作符使得它可以像函数一样调用。
    假定某个类有一个重载的 operator(),而且重载的 operator()要求获取一个参数,我们就将这个类称为“一元仿函数”(unary functor);相反,如果重载的 operator()要求获取两个参数,就将这个类称为“二元仿函数”(binary functor)。

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    class FuncObject01
    {
    public:
        void operator()()
        {
            cout << "Hello WOrld!" << endl;
        }
    };
    
    void FuncObject02()
    {
        cout << "Hello WOrld!" << endl;
    }
    
    void test01()
    {
        FuncObject01 fobj;
        fobj();
        FuncObject02();
        cout << "------------" << endl;
    }
    
    class FuncObject03
    {
    public:
        int operator()(int a, int b)
        {
            return a + b;
        }
    };
    
    int FuncObject04(int a, int b)
    {
        return a + b;
    }
    
    // 函数对象也可以像普通函数一样 具有返回值和参数
    void test02()
    {
        FuncObject03 fobj;
        int ret = fobj(10, 20);
        cout << "ret = " << ret << endl;
        ret = FuncObject04(10, 30);
        cout << "ret = " << ret << endl;
        cout << "------------------------" << endl;
    }
    
    // 函数对象超出了普通函数的功能,可以具有保存函数调用状态
    // 例如 我们要统计函数的调用次数
    
    class FuncObject05
    {
    public:
        FuncObject05() : count(0) {}
        void operator()()
        {
            cout << "Hello World!" << endl;
            count++;
        }
        int count;
    };
    
    // 普通函数统计调用次数,需要一个全局变量
    int g_count = 0;
    void FuncObject06()
    {
        cout << "Hello World!" << endl;
        g_count++;
    }
    
    void test03()
    {
        FuncObject06();
        FuncObject06();
        cout << "函数调用次数:" << g_count << endl;
        // 使用函数对象不需要使用全局变量
        FuncObject05 fobj;
        fobj();
        fobj();
        fobj();
        cout << "函数调用次数:" << fobj.count << endl;
        cout << "---------------------------" << endl;
    }
    
    // 函数对象做参数和返回值
    class print
    {
    public:
        print() : count(0){}
        void operator()(const int& val)
        {
            cout << val << " ";
            count++;
        }
        int count;
    };
    
    int num = 0;
    void print2(int val)
    {
        cout << val << " ";
        num++;
    }
    
    void test04()
    {
        vector<int> v;
        v.push_back(1);
        v.push_back(3);
        v.push_back(5);
        v.push_back(2);
        // 通过for_each算法 遍历容器元素
        print myprint;
        // 函数对象做返回值和参数
        myprint = for_each(v.begin(), v.end(), myprint);
        cout << endl;
        cout << "函数调用次数:" << myprint.count << endl;
        for_each(v.begin(), v.end(), print2);
        cout << endl;
        cout << "函数调用次数:" << num << endl;
    }
    
    int main()
    {
        test01();
        test02();
        test03();
        test04();
        getchar();
        return 0;
    }
  • 相关阅读:
    centos7 安装中文编码
    docker一些命令
    bash: ifconfig: command not found
    sublime3的licence(update 2016-04-14)
    Mac下更改python版本为3.5
    BigDecimal的equals
    cvc-elt.1: 找不到元素 'beans' 的声明
    Configuration problem: Only one AsyncAnnotationBeanPostProcessor may exist within the context.
    You need tcl 8.5 or newer in order to run the Redis test
    自定义的类型转换器中怎样自定义错误消息?(待解答)
  • 原文地址:https://www.cnblogs.com/duxie/p/10939600.html
Copyright © 2011-2022 走看看