函数对象,谓词,内建函数对象,适配器

函数对象(仿函数)

重载函数调用操作符号的类,就是类似于函数的对象,其实就是重载了()这个操作符,使类对象可以像函数那样使用,其中需要一个参数称为一元仿函数,两个参数为二元仿函数

class Print
{
public:
	void operator()()
	{
		cout <<"hellow world"<<endl;
	}
};
void pri ()
{
	cout<<"hellow world"<<endl;
}
int main
{
	Print p;
	p(); //我们可以看见重载()他的使用和函数很像,所以就叫仿函数
	pri()
}
谓词

谓词是指“普通函数”或者重载()的仿函数为bool类型,举一个例子,就是我们在使用find_if这个算法的时候,就可以用到谓词

class Find
{
public:
	bool operator()(int va)
	{
		return va>5;
	}
}
int main ()
{
	vector<int> v(10,2);
	vevtor<int>::iterator it=find_if(v.begin(),v.end(),Find());
	if (it!=v.end())
		cout<<"find"<<endl;
	else
		cout<<"no"<<endl;
	//使用谓词就可以找到需要的方法
}
内建函数对象

STL内建了一些函数对象,分为算法函数对象,关系运算类函数对象,逻辑运算类仿函数,这些仿函数所产出的对象,用法和一般函数相同,使用内建函数对象需要包含头文件

6 个算数类函数对象,除了 negate 是一元运算,其他都是二元运算。
template<class T> T plus<T>//加法仿函数
template<class T> T minute<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 test2()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
    negate<int>n;
    cout <<"取反   "<< n(10) << endl;//使用取反仿函数
    plus<int>p;
    cout << "相加   " << p(10,20) << endl;
    sort(v.begin(), v.begin(), greater<int>());//使用从大到小

}
适配器

他的作用就是将不同的接口转化为不同的接口,使接口兼容

函数对象适配器
class two_class :public binary_function<int, int, void>
{

public:
    void operator()(int val,int num) const
    {
        cout << val +num<< "  ";
    }
  
};

void test3()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
    for_each(v.begin(), v.end(), bind2nd(two_class(),10));
//1、利用bind2nd 进行绑定
//2、继承 public binary_function<参数1 类型,参数2类型,返回值类型>
//如果是一个参数的话就使用unary_function
//3、加const
取反适配器
void test4()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
    sort(v.begin(), v.end(), not2(less<int>()));
    for_each(v.begin(), v.end(), [](int val) { cout << val << "  "; });
}
//not1 是一元取反
//not2 是二元取反
函数适配器
void test03()
{
	vector<int>v;

	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	//将函数指针 适配成函数对象  ptr_fun
	for_each(v.begin(), v.end(), bind2nd(ptr_fun(myPrint3), 1000));
}

函数适配器

函数适配器就是可以将类成员函数变成仿函数供我们使用

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	void showPerson()
	{
		cout << "成员函数----姓名: " << this->m_Name << " 年龄: " << this->m_Age << endl;
	}

	void addAge()
	{
		this->m_Age += 100;
	}

	string m_Name;
	int m_Age;
};

//void myPrint4( Person & p)
//{
//	cout << "姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
//}

void test04()
{
	vector< Person > v;

	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	//利用 mem_fun_ref
	for_each(v.begin(), v.end(),  mem_fun_ref(&Person::showPerson));
	for_each(v.begin(), v.end(), mem_fun_ref(&Person::addAge));
	for_each(v.begin(), v.end(), mem_fun_ref(&Person::showPerson));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值