构建函数对象,谓词,内建函数对象的使用

1函数对象的概念:重载函数调用操作符的类,其对象称为函数对象

函数对象使用重载的()时,行为类似函数调用,也叫仿函数

本质:函数对象(仿函数)是一个类,不是一个函数

仿函数就是类(//struct结构体也可以)里面重载()操作符

2.使用的特点:

函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值

函数对象超出普通函数的概念,函数对象可以有自己的状态

函数对象可以作为参数传递

class MyAdd//struct结构体也可以
{
public:
	int operator()(int val1,int val2)
	{
		return val1 + val2;
	}
};
class MyPrint
{
public:
	MyPrint()
	{
		this->count = 0;
	}
	void operator()(string str)
	{
		cout << str << endl;
		count++;
	}
	int count;
};
void DoPrint(MyPrint &myprint,string str)//全局函数
{
	myprint(str);
}
void test01()
{
	MyAdd myadd;
	cout << myadd(10, 20) << endl;//函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
}
void test02()//函数对象超出普通函数的概念,函数对象可以有自己的状态(属性)
{
	MyPrint myprint;
	myprint("hellow world");
	myprint("hellow world");
	myprint("hellow world");
	myprint("hellow world");
	cout << "myprint调用的次数:" << myprint.count<< endl;
}
void test03()
{
	MyPrint myprint;
	DoPrint(myprint, "我勒个扫杠");//函数对象可以作为参数传递
}

2.谓词

概念:返回bool类型的仿函数称为谓词

operator()接收一个参数的叫一元谓词,接收两个参数的叫二元谓词

一元谓词:

class GreatFive
{
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 it=find_if(v.begin(), v.end(), GreatFive());
	if (it == v.end())
	{
		cout << "未找到" << endl;
	}
	else
	{
		cout << "找到了,是" << *it << endl;
	}
}

二元谓词:

class MyCompare
{
public:
	bool operator()(int val1,int val2)//二元谓词
	{
		return val1 > val2;
	}
};
void test01()
{
	vector<int> v;
	v.push_back(30);
	v.push_back(20);
	v.push_back(50);
	v.push_back(10);
	v.push_back(40);
	sort(v.begin(), v.end());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	//降序后
	sort(v.begin(), v.end(), MyCompare());//匿名函数对象
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

3.内建函数对象

概念:STL内建了一些函数对象

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

这些仿函数所产生的对象,用法和一般函数完全相同,用时要引入头文件#include<functional>

3.1算数仿函数

negate是一元运算,其他是二元

 

void test01()
{
	negate<int> m;
	cout << m(50) << endl;//-50(取反)
	plus<int> add;
	cout << add(10, 20) << endl;
}

3.2关系仿函数

void test01()
{
	vector<int> v;
	v.push_back(30);
	v.push_back(20);
	v.push_back(50);
	v.push_back(10);
	v.push_back(40);
	sort(v.begin(), v.end(), greater<int>());//用内置函数对象实现降序//greater<int>()匿名对象
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

最常用的就是大于,因为默认的就是小于

3.3逻辑仿函数

逻辑与:都真为真

逻辑或:有真则真

逻辑非:真则假,假则真

 

void test01()
{
	vector<bool> v;
	v.push_back(true);
	v.push_back(false);
	v.push_back(0);
	v.push_back(1);
	v.push_back(0);//1 0 0 1 0
	for (vector<bool>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	//利用逻辑非,将容器v中的数据搬运到容器v2中,并取反操作
	vector<bool> v2;
	v2.resize(v.size());//要先扩展大小,不然搬运不了//0 0 0 0 0
	transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());//0 1 1 0 1
	for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值