• C++仿函数




    仿函数[bool 谓词和二元谓词]

    参数有一个的返回值为bool的数据类型,为一元谓词
    参数有两个的返回值为bool的数据类型,二元谓词

    #include 
    #include 
    #include 
    #include
    using namespace std;
    //仿函数 返回值为bool的数据类型 为谓词
    class stu {
    public:
    	//参数有一个的时一元谓词
    	bool operator()(int val) {
    		return val > 5;
    	}
    	bool operator()(int val1, int val2) {
    		return val1 > val2;
    	}
    	
    };
    void show(vector <int> ret) {
    	for (auto a : ret)
    		cout << a << "   ";
    }
    int main() {
    	vector <int>ret;
    	for (int i = 0; i < 10; i++) {
    		ret.push_back(rand() % 100);
    	}
    	//vector ::iterator
    	auto isok = find_if(ret.begin(), ret.end(), stu());
    	if (isok == ret.end())
    		cout << "没找到" << endl;
    	else
    		cout << "找到了" << *isok << endl;
    	sort(ret.begin(), ret.end(), stu());
    	show(ret);
    }
    
    • 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

    在这里插入图片描述

    算术仿函数 include functional

    一元运算符

    negate

    二元运算符

    • ‘+’ plus
    • ‘-’ minus
    • ‘x’ multiplies
    • ‘/’ divides
    • '%'modulus
    #include 
    #include 
    #include
    #include 
    using namespace std;
    
    void test01() {
    	negate <int> n;//取反
    	cout << n(50)<<endl;
    }
    void test02() {
    	plus <int> n;
    	cout << n(10, 20)<<endl;
    }
    int main() {
    	test01();
    	test02();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    关系仿函数[头文件functional]

    大于 greater
    大于等于 geater_equal
    小于 less
    小于等于 less_equal
    等于 equal_to
    不能与 not_equal_to

    #include 
    #include 
    #include
    #include 
    using namespace std;
    
    void show(vector <int> &ret) {
    	for (vector<int>::iterator it = ret.begin(); it != ret.end(); it++)
    		cout << *it << "   ";
    	cout << endl;
    }
    vector <int> init() {
    	vector <int> ret;
    	for(int i=0;i<10;i++)
    		ret.push_back(rand() % 50);
    	return ret;
    }
    int main() {
    	vector <int> ret;
    	ret = init();
    	show(ret);
    	//内建函数对象  greater用于排序
    	sort(ret.begin(), ret.end(), greater<int>());
    	show(ret);
    }
    
    • 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

    在这里插入图片描述

    逻辑仿函数

    logical_and 和
    logical_not 非
    logical_or 或

    #include 
    #include 
    #include
    #include 
    using namespace std;
    
    void show(vector <bool>ret) {
    	for (vector<bool>::iterator it = ret.begin(); it != ret.end(); it++)
    		cout << *it << "   ";
    	cout << endl;
    }
    vector <bool> init() {
    	vector <bool> ret;
    	for(int i=0;i<10;i++)
    		ret.push_back(rand() % 2);
    	return ret;
    }
    int main() {
    	vector <bool> ret;
    	ret = init();
    	show(ret);
    	//内建函数对象  greater用于排序
    	vector <bool>ret1(ret);
    	transform(ret.begin(), ret.end(),ret1.begin(),logical_not<bool>());
    	show(ret1);
    }
    
    • 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

    在这里插入图片描述

  • 相关阅读:
    ASO优化关键词覆盖、曝光原理
    Hive 常用存储、压缩格式
    L1-020 帅到没朋友(Python3)
    性能测试(基于Jmeter)
    React(2)
    第一章 CIS 安全基准-网络访问控制
    【基础】Java面试题
    ShowLayoutAlias...
    问题解决:Docker:IPv4 forwarding is disabled
    【力扣-每日一题】LCP 06. 拿硬币
  • 原文地址:https://blog.csdn.net/weixin_45646601/article/details/127694376