• 【C++】STL-函数对象 + 谓词


    1.函数对象使用

    在这里插入图片描述
    在这里插入图片描述

    #include 
    using namespace std;
    
    //STL-函数对象(仿函数)
    
    class MyAdd
    {
    public:
    	int operator()(int v1, int v2)
    	{
    		return v1 + v2;
    	}
    };
    //1、函数对象在使用时,可以像普通函数那用调用,可以有参数,可以有返回值
    void test01()
    {
    	MyAdd myAdd;
    	cout << myAdd(10, 10) << endl; 
    }
    
    // 2、函数对象超出普通函数的概念,函数对象可以有自己的状态
    class MyPrint
    {
    public:
    	MyPrint()
    	{
    		this->m_count = 0;
    	}
    
    	void operator()(string test)
    	{
    		cout << test << endl;
    		this->m_count++;
    	}
    
    	int m_count; // 内部自己状态
    };
    
    void test02()
    {
    	MyPrint myPrint;
    	myPrint("Hello World!");
    	myPrint("Hello World!");
    	myPrint("Hello World!");
    	myPrint("Hello World!");
    
    	cout << "MyPrint调用的次数: " << myPrint.m_count << endl;
    }
    
    void doPrint(MyPrint& mp, string test)
    {
    	mp(test);
    }
    
    // 3、函数对象可以作为参数传递
    void test03()
    {
    	MyPrint myPrint;
    	doPrint(myPrint, "Hello C++!");
    }
    
    int main()
    {
    	test01();
    	cout << "------------------------" << endl << endl;
    	test02();
    	cout << "------------------------" << endl << endl;
    	test03();
    
    	//**************************************
    	system("pause");
    	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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    在这里插入图片描述

    2.谓词-一元谓词

    在这里插入图片描述
    在这里插入图片描述

    #include 
    using namespace std;
    
    //STL-一元谓词
    #include
    #include
    
    class GreaterFive
    {
    public:
    	bool operator()(int val)
    	{
    		return val > 5;
    	}
    };
    
    void test01()
    {
    	vector<int>v;
    	for (int i = 0; i < 10; i++)
    	{
    		v.push_back(i);
    	}
    
    	//查找容器中 有没有大于5的数字
    	//GreaterFive() 匿名函数对象
    	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
    	if (it == v.end())
    	{
    		cout << "未找到!" << endl;
    	}
    	else
    	{
    		cout << "找到了大于5的数字为:" << *it << endl;
    	}
    }
    
    int main()
    {
    	test01();
    	cout << "------------------------" << endl << endl;
    	//test02();
    	//cout << "------------------------" << endl << endl;
    	//test03();
    
    	//**************************************
    	system("pause");
    	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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    在这里插入图片描述

    3.二元谓词

    在这里插入图片描述

    #include 
    using namespace std;
    
    //STL-二元谓词
    
    #include
    #include
    
    class MyCompare
    {
    public:
    	bool operator()(int val1, int val2) //参数为两个 , 二元谓词
    	{
    		return val1 > val2;  //降序
    	}
    };
    
    void test01()
    {
    	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 (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    	{
    		cout << *it << " ";
    	}
    	cout << endl;
    
    	cout << "------------------------" << endl << endl;
    	//使用函数对象 改变算法策略,变为排序规则 从大到小
    	sort(v.begin(), v.end(), MyCompare());
    	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    	{
    		cout << *it << " ";
    	}
    	cout << endl;
    }
    
    int main()
    {
    	test01();
    	cout << "------------------------" << endl << endl;
    	//test02();
    	//cout << "------------------------" << endl << endl;
    	//test03();
    
    	//**************************************
    	system("pause");
    	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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    在这里插入图片描述

  • 相关阅读:
    docker部署(使用docker-compose)手把手教程
    基于非支配排序遗传算法NSGAII的综合能源优化调度(Matlab代码实现)
    C笔记:引用调用,通过指针传递
    OpenCV---视频操作
    做外贸真诚是最好的套路
    LeetCode 276:栅栏涂色
    关于原型交互设计文档的一些建议
    记一次 .NET 某医疗住院系统 崩溃分析
    考公顺序步骤
    分析脚手架、ref、props
  • 原文地址:https://blog.csdn.net/m0_48808835/article/details/132686813