函数对象、谓词

1. 函数对象

    就是类中重载了(),operator()函数,可以像函数一样调用,所以我们也把函数对象叫做仿函数

class Myadd
{
public:
	int operator()(int a, int b)
	{
		return a + b;
	}
};
void  test01()
{
	Myadd p;
	cout << p(2, 3) << endl;
	cout << Myadd()(4, 5) << endl;
}
void  print(int a)
{
	cout << a << endl;
}
class Print
{
public:
	void  operator()(int a)
	{
		cout << a << endl;
	}
};
bool compare(int a, int b)
{
	return a > b;
}
class Compare
{
public:
	bool operator()(int a, int b)
	{
		return a > b;
	}

};
void test02()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(3);
	v.push_back(2);
	v.push_back(5);
	sort(v.begin(), v.end(), Compare());
	for_each(v.begin(),v.end(), Print());

}

2. 谓词

谓词是指普通函数或重载的operator()返回值是bool类型的函数对象(仿函数)。如果operator 接受一个参数,那么叫做一元谓词,如果接受两个参数,那么叫做二元谓词,谓词可作为一个判断式。 

class Print
{
public:
	void  operator()(int a)
	{
		cout << a << endl;
	}
};
bool greater2(int a)
{
	return a > 2;
}
void test01()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(3);
	v.push_back(2);
	v.push_back(5);
	vector<int>::iterator it =  find_if(v.begin(), v.end(), greater2);
	if (it != v.end())
	{
		cout << *it << endl;
	}
	for_each(v.begin(), v.end(), Print());

}

3. 内建函数对象

STL内建了一些函数对象。分为:算数类函数对象、关系运算类函数对象、逻辑运算类仿函数。这些仿函数所产生的对象,用法和一般函数完全相同,当然我们还可以产生无名的临时对象来履行函数功能。使用内建函数对象,需要引入头文件 #include <functional>。

6个算数类函数对象,除了negate是一元运算,其他都是二元运算。

template<class T> T plus<T> //加法仿函数

template<class T> T minus<T> //减法仿函数

template<class T> T multiplies<T> //乘法仿函数

template<class T> T divides<T> //除法仿函数

template<class T> T modulus<T> //取模仿函数

template<class T> T negate<T> //取反仿函数

6个关系运算类函数对象,每一种都是二元运算。

template<class T> bool equal_to<T> //等于

template<class T> bool not_equal_to<T> //不等于

template<class T> bool greater<T> //大于

template<class T> bool greater_equal<T> //大于等于

template<class T> bool less<T> //小于

template<class T> bool less_equal<T>//小于等于

逻辑运算类运算函数,not为一元运算,其余为二元运算。

template<class T> bool logical_and<T> //逻辑与

template<class T> bool logical_or<T> //逻辑或

template<class T> bool logical_not<T> //逻辑非

void  test01()
{
	negate<int> p;
	cout << p(5) << endl;
	cout << negate<int>()(2) << endl;

}
void  test02()
{
	cout << plus<int>()(2, 3) << endl;
}
class Print
{
public:
	void  operator()(int a)
	{
		cout << a << endl;
	}
};
void test03()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(3);
	v.push_back(2);
	v.push_back(5);
	//sort(v.begin(), v.end(), greater<int>());
	sort(v.begin(), v.end(),less<int>());
	for_each(v.begin(), v.end(), Print());

}

4. 适配器

     用来适配参数,扩展参数接口,一般结合仿函数一起使用

using namespace std;
//一元继承public unary_function<参数1 ,返回值类型>
//二元继承public  binary_function<int,int,void>

class Print:public   binary_function<int, int, void>
{
	public:
		void operator()(int  a ,int  num)const
		{
			cout << a << " " << num << endl;
			cout << a +num<< endl;
		}
};
void  test()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	//绑定参数  bind2nd  
	//bind1st
	for_each(v.begin(),v.end(), bind2nd(Print(),200));
	//for_each(v.begin(), v.end(), bind1st(Print(), 200));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值