STL函数对象(仿函数)/谓词

1、概念。
重载函数调用操作符的类,其对象称为函数对象
函数对象使用重载()时,行为类似函数调用,也叫仿函数

2、本质。
函数对象(仿函数)是一个类的对象,不是一个函数。

例如:
class Person{
public:
    void operator()()
    {
    }
};

Person p;    p就是一个函数对象
p();         函数对象使用()时,非常像函数的调用,所以称为仿函数。

函数对象的使用
特点:
1、函数对象在使用时,可以像普通函数那样调用,也可以有参数,可以有返回值。
2、函数对象超过普通函数的概念,函数对象可以有自己的状态。
3、函数对象可以作为参数传递。

#include <iostream>
using namespace std;

/*
特点:
1、函数对象在使用时,可以像普通函数那样调用,也可以有参数,可以有返回值。
2、函数对象超过普通函数的概念,函数对象可以有自己的状态。
3、函数对象可以作为参数传递。
*/

//1、函数对象在使用时,可以像普通函数那样调用,也可以有参数,可以有返回值。
class MyAdd{
public:
	int operator()(int v1,int v2)
	{
		return v1 + v2;
	}
};

void test01()
{
	MyAdd myadd;                      //myadd就是函数对象
	int ret = myadd(10,20);
	cout << "ret = " << ret << endl;  //30
}

//2. 函数对象超过普通函数的概念,函数对象可以有自己的状态。
class Myprint{
public:
	Myprint()
	{
		this->count = 0;
	}

	void operator()(string text)
	{
		cout << text << endl;
		this->count++;
	}

	int count;
};

void test02()

	Myprint myprint;      //函数对象
	myprint("helloworld");
	myprint("helloworld");
	myprint("helloworld");
	myprint("helloworld");
	myprint("helloworld");
	myprint("helloworld");
	myprint("helloworld");
	myprint("helloworld");

	cout << "myprint调用的次数是:" << myprint.count << endl;
}

//3. 函数对象可以作为参数传递。
void doPrint(Myprint &mp,string text)
{
	mp(text);
}


void test03()
{
	Myprint myprint;
	doPrint(myprint,"hello C++");
}

int main(int argc,char *argv[])
{
	//test01();
	//test02();
	test03();
	return 0;
}

谓词

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

#include <iostream>    /*标准io*/
using namespace std;   /*命名空间*/
#include <vector>      /*vector*/
#include <algorithm>   /*算法*/

//一元谓词
//
//find_if(iterator beg,iterator end,_Pred);
//按条件查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
//beg  开始迭代器
//end  结束迭代器
//_Pred 函数或者谓词(返回bool类型的仿函数)

class GreaterFive{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};

int main(int argc,char *argv[])
{
	vector<int> v;
	int i;
	for(i=0;i<10;i++)
	{
		v.push_back(i);
	}

	//查找容器中有没有大于5的数字。
	//GreaterFive g;
	//vector<int>::iterator it = find_if(v.begin(),v.end(),g);  //填函数对象
	
	vector<int>::iterator it = find_if(v.begin(),v.end(),GreaterFive());  
    //GreaterFive()匿名对象

	if(it == v.end())
	{
		cout << "未找到" << endl;
	}
	else{
		cout << "找到了该数字:" << *it << endl;
	}

	return 0;
}
#include <iostream>     /*标准io*/
using namespace std;    /*命名空间*/
#include <algorithm>    /*s算法*/
#include <vector>       /*vector*/

//二元谓词

class Mycompare{
public:
	bool operator()(int v1,int v2)
	{
		return v1 > v2;
	}
};

int main(int argc,char *argv[])
{
	vector<int> v;      
	v.push_back(10);    /*尾插*/
	v.push_back(40);
	v.push_back(30);
	v.push_back(20);
	v.push_back(50);

	sort(v.begin(),v.end());    /*排序*/

	for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	//使用函数对象,改变算法策略,变成排序规则是从大到小
	//Mycompare mc;
	//sort(v.begin(),v.end(),mc);
	sort(v.begin(),v.end(),Mycompare());

	for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

物の哀

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

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

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

打赏作者

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

抵扣说明:

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

余额充值