《C++ SYNTAX》第16章 函数对象

本文介绍了C++中的仿函数(函数对象)概念,包括算术、关系和逻辑仿函数,并通过实例展示了如何定义和使用它们。同时,详细讲解了谓词的类型,如一元和二元谓词,并利用STL中的仿函数进行了排序和查找操作。此外,还提及了内建的算术、关系和逻辑函数对象,以及它们在实际编程中的应用。
摘要由CSDN通过智能技术生成

重载了函数调用操作符的类,其对象称为函数对象。特别地,函数对象使用重载的()时,行为类似函数调用,也叫仿函数

#include<iostream>
#include<string>
using namespace std;

class MyAdd
{
public:
	int operator()(const int a, const int b)
	{
		return a + b;
	}
};

class MyPrint
{
public:
	int count; //记录调用次数
public:
	MyPrint()
	{
		this->count = 0; //初始化调用次数为0
	}
	void operator()(const string str)
	{
		cout << str << endl;
		this->count++; //调用次数加1
	}
};

void doPrint(MyPrint& mp, string str)
{
	mp(str);
}

int main()
{
	MyAdd myAdd; //创建函数对象
	cout << myAdd(10, 20) << endl; //(1)函数对象可以像普通函数那样调用,可以有参数和返回值
	MyPrint myPrint;
	myPrint("Hello!");
	myPrint("Hello!");
	myPrint("Hello!");
	cout << "myPrint的调用次数为:" << myPrint.count << endl; //(2)函数对象可以有自己内部的状态(输出3)
	doPrint(myPrint, "Hello!"); //(3)函数对象可以作为参数进行传递
	system("pause");
	return 0;
}

(16.1) 谓词

返回bool类型的仿函数称为谓词。如果operator()接受一个参数,那么叫做一元谓词;如果operator()接受两个参数,那么叫做二元谓词

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class GtFive //一元谓词
{
public:
    bool operator()(const int a)
    {
        return a > 5;
    }
};

class MyCompare //二元谓词
{
public:
    bool operator()(const int a,const int b)
    {
        return a > b; //降序
    }
};

int main()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    //find_if算法第三个参数是谓词,GtFive()是匿名函数对象
    vector<int>::iterator it = find_if(v.begin(), v.end(), GtFive()); //查找容器中大于5的数,返回迭代器
    if (it == v.end())
    {
        cout << "没有找到。" << endl;
    }
    else
    {
        cout << *it << endl; //6
    }
    sort(v.begin(), v.end()); //默认升序排列
    //sort算法的第三个参数也是谓词,MyCompare()是匿名函数对象
    sort(v.begin(), v.end(), MyCompare()); //使用函数对象改变排序策略
    system("pause");
    return 0;
}

(16.2) 算术仿函数

STL也提供了一些仿函数,这些仿函数被称为内建函数对象。有算术仿函数关系仿函数逻辑仿函数。需要包含头文件#include<functional>

#include<iostream>
#include<functional>
using namespace std;
int main()
{
    //取相反数仿函数
    negate<int> n;
    cout << n(10) << endl; //-10
    //取余仿函数
    modulus<int> md;
    cout << md(13, 5) << endl;; //3
    //加法仿函数
    plus<int> p;
    cout << p(10, 20) << endl; //30
    //减法仿函数
    minus<int> m;
    cout << m(30, 10) << endl; //20
    //乘法仿函数
    multiplies<int> ml;
    cout << ml(20, 30) << endl; //600
    //除法仿函数
    divides<int> d;
    cout << d(100, 10) << endl; //10
    system("pause");
    return 0;
}

(16.3) 关系仿函数

#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{
    vector<int> v;
    v.push_back(20);
    v.push_back(10);
    v.push_back(30);
    sort(v.begin(), v.end(), greater<int>()); //用STL提供的关系仿函数,实现降序排序
    //其他STL关系仿函数:
    //equal_to<T>(); //等于
    //not_equal_to<T>(); //不等于
    //greater<T>(); //大于
    //less<T>(); //小于
	//greater_equal<T>(); //大于等于
	//less_equal<T>(); //小于等于
    system("pause");
    return 0;
}

(16.4) 逻辑仿函数

#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{
    vector<bool> v1;
    v1.push_back(true);
    v1.push_back(false);
    vector<bool> v2;
    v2.resize(v1.size());
    transform(v1.begin(), v1.end(), v2.begin(), logical_not<bool>()); //将v1搬运到v2中并取反
    //其他STL逻辑仿函数:
    //logical_and<T>(); //与
    //logical_or<T>(); //或
    //logical_not<T>(); //非
    system("pause");
    return 0;
}

《 C + +   S Y N T A X 》 系 列 博 客 创 作 参 考 资 料 来 源 《C++\ SYNTAX》系列博客创作参考资料来源 C++ SYNTAX

  1. 《黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难》@黑马程序员.https://www.bilibili.com.

博 客 创 作 : A i d e n   L e e 博客创作:Aiden\ Lee Aiden Lee
特别声明:文章仅供学习参考,转载请注明出处,严禁盗用!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值