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;
    }
  • 相关阅读:
    像素与豪米的转换
    C#中的事件和委托
    .Datagridview数据写入DataTable
    C# winform DataGridView 常见属性
    c# winform 用代码修改DataGridView列头的名字,设置列名,修改列名
    SVN常用命令
    (装载)C#中AppDomain.CurrentDomain.BaseDirectory与Application.StartupPath的区别
    C# WinForm dataGridView 技巧小结
    IPV4二进制显示
    Microsoft SQL Server 2005技术内幕:TSQL查询 PerformanceDB.sql
  • 原文地址:https://www.cnblogs.com/duxie/p/10939600.html
Copyright © 2011-2022 走看看