
目录
●仿函数
1.概念
2.使用
●谓词
1.一元谓词
2.二元谓词
●内建仿函数
1.算数仿函数
2.关系仿函数
3.逻辑仿函数
●仿函数
1.概念
重载函数调用操作符的类,其对象常称为函数对象。函数对象使用重载的()时,行为类似函数的调用,所以也叫仿函数。它的本质为一个类,而不是一个函数。
2.使用
功能特点:
1.仿函数在使用时可以像普通函数那样调用,可以有参数和返回值
#include<iostream>
using namespace std;
//仿函数在使用时可以像普通函数那样调用,可以有参数和返回值
class add {
public:
	int operator()(const int value1, const int value2)
	{
		return value1 + value2;
	}
};
class sub {
public:
	int operator()(const int value1, const int value2)
	{
		return value1 - value2;
	}
};
class mul {
public:
	double operator()(const double value1,const double value2)
	{
		return value1 * value2;
	}
};
void text()
{
	add ad;
	cout << "相加:" << ad(10, 20) << endl;
	sub sb;
	cout << "相减:" << sb(10, 20) << endl;
	mul ml;
	cout << "相乘:" << ml(5.5,7) << endl;
}
int main()
{
	text();
}
2.仿函数不同于普通函数的概念,它可以有自己的状态
#include<iostream>
using namespace std;
class print {
public:
	void operator()(const int value1,const int value2)//仿函数不同于普通函数的概念,它可以有自己的状态
	{
		cout << "相加:" << value1 + value2 << endl;
		cout << "相减:" << value1 - value2 << endl;
		cout << "相乘:" << value1 * value2 << endl;
	}
};
void text()
{
	print pt;
	pt(10,20);
}
int main()
{
	text();
}
3.仿函数可以作为参数传递
#include<iostream>
using namespace std;
class print {
public:
	void operator()(const int value1, const int value2)
	{
		cout << "相加:" << value1 + value2 << endl;
		cout << "相减:" << value1 - value2 << endl;
		cout << "相乘:" << value1 * value2 << endl;
	}
};
void scanf(print &pt)//仿函数可以作为参数传递
{
	pt(10,20);
}
void text()
{
	print pt;
	scanf(pt);
}
int main()
{
	text();
}
●谓词
1.一元谓词
如果仿函数的返回值为bool类型并且operator()接受一个参数,则称它为一元谓词
#include<iostream>
#include<algorithm>  //STL算法头文件定义
#include<vector>
using namespace std;
class unitary {
public:
	bool operator()(const int value)  //一元仿函数
	{
		return value == 20;
	}
};
void text()
{
	vector<int>v;
	for (int i = 1, j = 10; i <= 10; i++, j += 10)
	{
		v.push_back(j);
	}
	//10 20 30 40 50 60 70 80 90 100
	vector<int>::iterator p = find_if(v.begin(), v.end(), unitary());  
	//利用find_if这个算法,在一元仿函数中去判断vector容器中是否有元素20
	if (p == v.end())
		cout << "未找到" << endl;
	else
		cout << "从vector容器中找到了值为20的元素" << endl;
}
int main()
{
	text();
}
2.二元谓词
如果仿函数的返回值为bool类型并且operator()接受两个参数,则称它为二元谓词
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void printvector(vector<int>&v)
{
	for (vector<int>::iterator i = v.begin(); i != v.end(); i++)
	{
		cout << *i<<" ";
	}
	cout << endl;
}
class compare {
public:
	bool operator()(const int value1,const int value2)  //二元仿函数
	{
		return value1 > value2;
	}
};
void text()
{
	vector<int>v;
	v.push_back(45);
	v.push_back(10);
	v.push_back(9);
	v.push_back(67);
	v.push_back(35);
	//45 10 9 67 35
	cout << "从小到大排序:";
	sort(v.begin(), v.end());
	//9 10 35 45 67
	printvector(v);
	cout << "从大到小排序:";
	sort(v.begin(), v.end(), compare());
	//67 45 35 10 9
	printvector(v);
}
int main()
{
	text();
}
●内建仿函数
1.算数仿函数
函数原型:
■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> //取反仿函数
#include<iostream>
#include<functional>  //内建仿函数头文件定义
using namespace std;
void text()
{
	//相加仿函数
	plus<int>p;
	cout << "相加:" << p(10, 20) << endl;
	//相减仿函数
	minus<float>mi;
	cout << "相减:" << mi(3.18,2.90) << endl;
	//乘法仿函数
	multiplies<double>mu;
	cout << "相乘:" << mu(9.19, 3.14) << endl;
	//除法仿函数
	divides<double>di;
	cout << "相除:" << di(25.75, 3.15) << endl;
	//取模仿函数
	modulus<int>mo;
	cout << "取模:" << mo(9,4) << endl;
	//取反仿函数
	negate<int>n;
	cout << "取反:" << n(1) << endl;
}
int main()
{
	text();
}
2.关系仿函数
函数原型:
         ■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> //小于等于
#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
using namespace std;
void printvector(vector<int>&v)
{
	for (vector<int>::iterator i = v.begin(); i != v.end(); i++)
	{
		cout << *i<<" ";
	}
	cout << endl;
}
void text()
{
	vector<int>v;
	cout << "请向vector容器中输入元素:" << endl;;
	for (int i = 1; i <= 10; i++)
	{
		int elem; cin>>elem;
		v.push_back(elem);
	}
	//常用关系仿函数
	//大于=降序
	sort(v.begin(),v.end(),greater<int>());
	printvector(v);
	//小于=升序
	sort(v.begin(), v.end(), less<int>());
	printvector(v);
}
int main()
{
	text();
}
3.逻辑仿函数
函数原型:(该仿函数基本不用,所以下面代码中只做简单了解)
■template<class T> bool logical and<T> //逻辑与
■template<class T> bool logical or<T> //逻辑或
■template<class T> bool logical not<T> //逻辑非
#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
void printvector(vector<bool>&v)
{
	for (vector<bool>::iterator i = v.begin(); i != v.end(); i++)
	{
		cout << *i<<" ";
	}
	cout << endl;
}
void text()
{
	vector<bool>v;
	v.push_back(true);
	v.push_back(false);
	cout << "初始状态:" << endl;
	printvector(v);
	
	vector<bool>v1;
	v1.resize(v.size());
	transform(v.begin(), v.end(), v1.begin(), logical_not<bool>());
	cout << "逻辑非后的状态:" << endl;
	printvector(v1);
}
int main()
{
	text();
}



















