• 函数对象的介绍及使用


    一、函数对象基本使用

    概念

    • 重载函数调用操作符的类,其创建的对象称为函数对象
    • 函数对象使用重载的()时,行为与函数调用相似,所以也将其称为仿函数

    特点

    • 函数对像使用时可以有参数和返回值
    • 函数对象超出普通函数的概念,可以有自己的状态
    • 函数对象可以作为参数传递
    #include<iostream>
    #include<string>
    using namespace std;
    class Add
    {
    public:
    	int operator()(int v1, int v2)
    	{
    		return v1 + v2;
    	}
    };
    void test01()
    {
    	Add add;
    	cout << add(10, 20) << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    这里自定义的Add类创建的对象add即为函数对象
    可以有返回值、参数,使用与函数无二,实现了两数相加

    class Print
    {
    public:
    	void operator()(string str)
    	{
    		cout << str << endl;
    		this->count++;
    	}
    	int count=0;
    };
    void test02()
    {
    	Print print;
    	print("Hello world!");
    	print("Hello world!");
    	print("Hello world!");
    	cout << "print调用次数:" << print.count << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    这里的类有自己的成员变量count,可以计算打印次数,这是普通函数所不具有的,函数对象可以有自己的状态

    void doPrint(Print&p, string str)
    {
    	p(str);
    }
    void test03()
    {
    	Print print;
    	doPrint(print, "Hello world!");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    这里创建了一个函数,把函数对象作为参数实现参数传递打印。

    二、谓词

    概念

    • 返回bool类型的仿函数被称为谓词
    • 如果operator()接收一个参数,则称为一元谓词
    • 如果operator()接收两个参数,则称为二元谓词

    一元谓词

    #include<iostream>
    #include<vector>
    #include<algorithm>
    using namespace std;
    class mfive
    {
    public:
    	bool operator()(int val)
    	{
    		return val > 5;
    	}
    };
    void test01()
    {
    	vector<int>v;
    	for (int i = 0; i < 10; i++)
    		v.push_back(i);
    	vector<int>::iterator i=find_if(v.begin(), v.end(), mfive());
    	if (i == v.end())
    		cout << "未找到" << endl;
    	else cout << "找到了大于5的数字为:" <<*i << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    find_if算法库函数,查找满足条件的值,在类中设置的为大于5的值,i解引用的值为6,返回的是第一次找到的值的对应迭代器

    二元谓词

    class Cmp
    {
    public:
    	bool operator()(int v1,int v2)
    	{
    		return v1 > v2;
    	}
    };
    void test02()
    {
    	vector<int>v;
    	v.push_back(10);
    	v.push_back(40);
    	v.push_back(20);
    	v.push_back(30);
    	v.push_back(50);
    	sort(v.begin(), v.end());
    	for (auto i : v)
    		cout << i << " ";
    	cout << endl;
    	cout << "---------------------------" << endl;
    	sort(v.begin(), v.end(),Cmp());
    	for (auto i : v)
    		cout << i << " ";
    	cout << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    利用重载运算符()实现降序排序

    在这里插入图片描述

    三、内置函数对象

    概念
    STL内建的函数对象
    分类

    • 算数仿函数
    • 关系仿函数
    • 逻辑仿函数

    用法
    和函数一致,需要引用头文件#include<functional>

    一、算数仿函数

    plus:加法仿函数
    minus:减法仿函数
    multiplies乘法仿函数
    divides除法仿函数
    modulus取模仿函数
    negate取反仿函数

    #include<iostream>
    #include<functional>
    using namespace std;
    void test01()
    {
    	negate<int>n;
    	cout << n(50) << endl;
    }
    void test02()
    {
    	plus<int>p;
    	cout << p(10, 20) << endl;
    }
    int main()
    {
    	test01();
    	test02();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    二、关系仿函数

    equal:等于
    not_equal:不等于
    greater:大于
    greater_equal:大于等于
    less:小于
    less_equal:小于等于

    #include<functional>
    #include<vector>
    #include<algorithm>
    using namespace std;
    class cmp
    {
    public:
    	bool operator()(int v1, int v2)
    	{
    		return v1 > v2;
    	}
    };
    void test01()
    {
    	vector<int>v;
    	v.push_back(10);
    	v.push_back(20);
    	v.push_back(40);
    	v.push_back(30);
    	v.push_back(50);
    	for (auto i : v)
    		cout << i << " ";
    	cout << endl;
    	//sort(v.begin(), v.end(), cmp());
    	sort(v.begin(), v.end(), greater<int>());
    	for (auto i : v)
    		cout << i << " ";
    	cout << endl;
    }
    int main()
    {
    	test01();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    我们可以使用内建的关系仿函数指定排序方式

    在这里插入图片描述
    此外,浏览sort函数的定义可以看到,默认排序下,sort传入了less<>(),因此sort函数默认排序为升序排序
    在这里插入图片描述

    三、逻辑仿函数

    logical_and:逻辑与
    logical_or:逻辑或
    logical_not:逻辑非

    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<functional>
    using namespace std;
    void test01()
    {
    	vector<bool>v1;
    	v1.push_back(true);
    	v1.push_back(false);
    	v1.push_back(true);
    	v1.push_back(false);
    	for (auto i : v1)
    		cout << i << " ";
    	cout << endl;
    	vector<bool>v2;
    	v2.resize(v1.size());
    	transform(v1.begin(), v1.end(), v2.begin(), logical_not<bool>());
    	for (auto i : v2)
    		cout << i << " ";
    	cout << endl;
    }
    int main()
    {
    	test01();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    上面代码是将v1的值转移到v2上(当然v1的值还在),并对值进行逻辑非的取反操作,transform算法库函数,将v1从begin到end的值拷贝转移到以v2.begin为起点的内存空间上,因此要注意,运输的目标对象必须事先开辟足够的空间来接收,否则运行报错。
    在这里插入图片描述

  • 相关阅读:
    Unity协同程序
    网络卡顿怎么办?快来试试华为云CDN
    three.js 航拍全景图(+陀螺仪)
    QtC++与QTableView详解
    2022.10.29每日一题
    数字IC验证——PSS可移植测试用例
    Git GitHub GitLab
    了解什么是架构基本概念和架构本质
    不止于奶茶,香飘飘释放双轮驱动协同优势
    品牌平面设计如何让自己有脑洞大开的创意 优漫动游
  • 原文地址:https://blog.csdn.net/qwer1234mnbv_/article/details/125606535