函数对象的介绍及使用

一、函数对象基本使用

概念

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

特点

  • 函数对像使用时可以有参数和返回值
  • 函数对象超出普通函数的概念,可以有自己的状态
  • 函数对象可以作为参数传递
#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;
}

这里自定义的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;
}

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

void doPrint(Print&p, string str)
{
	p(str);
}
void test03()
{
	Print print;
	doPrint(print, "Hello world!");
}

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

二、谓词

概念

  • 返回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;
}

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;
}

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

在这里插入图片描述

三、内置函数对象

概念
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;
}

在这里插入图片描述

二、关系仿函数

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;
}

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

在这里插入图片描述
此外,浏览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;
}

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

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

...404 Not Found

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值