4.1 函数对象
重载函数调用操作符的类,其对象称为函数对象。函数对象使用重载的()
时,行为类似函数的调用,所以称为仿函数。本质上,函数对象(仿函数)是一个类,而不是一个函数。
函数对象的使用:
- 函数对象可以像普通函数那样调用,可也有参数,也可以有返回值。
- 函数对象超出普通函数的概念,函数对象可以有自己的状态。
- 函数对象可以作为参数传递
示例:
#include<iostream>
using namespace std;
#include<string>
// 1. 函数对象可以像普通函数那样调用,可也有参数,也可以有返回值。
class MyAdd
{
public:
int operator()(int v1,int v2)
{
return v2+v2;
}
};
void test01()
{
MyAdd myAdd; // myAdd即为函数对象
cout << myAdd(10,10) << endl;
}
// 2. 函数对象超出普通函数的概念,函数对象可以有自己的状态。
// 对于一个普通函数,如果想要统计函数执行次数,需要定义一个全局变量
// 仿函数则可以直接在类内定义属性来记录函数执行次数
class MyPrint
{
public:
MyPrint()
{
this->count = 0;
}
void operator()(string text)
{
cout << text << endl;
this->count++;
}
int count; // 内部自己的状态
};
void test02()
{
MyPrint myPrint;
myPrint("Hello World!");
myPrint("Hello World!");
myPrint("Hello World!");
cout << "MyPrint调用次数:" << myPrint.count << endl;
}
// 3. 函数对象可以作为参数传递
void doPrint(MyPrint &mp,string text)
{
mp(text);
}
void test03()
{
MyPrint myPrint;
doPrint(myPrint,"Hello!");
}
int main()
{
test01();
test02();
test03();
system("pause");
return 0;
}
4.2 谓词
返回bool
类型的仿函数称为谓词。
如果operator()
接收一个参数,那么叫做一元谓词;如果operator()
接收两个参数,那么叫做二元谓词。
4.2.1 一元谓词
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class GreaterFive
{
public:
bool operator()(int val)
{
return val>5;
}
};
void test01()
{
vector<int> v;
for(int i = 0;i<10;i++)
{
v.push_back(i);
}
// 查找容器中有没有大于5的数字
vector<int>::iterator it = find_if(v.begin(),v.end(),GreaterFive());
//GreaterFive()为匿名对象
if(it==v.end())
{
cout << "未找到!" << endl;
}
else
{
cout << "找到的大于5的数字为:" << *it << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
这里用到了新的算法find_if
,需要传三个参数:用迭代器表示的起始位置和终止位置(区间),以及一个谓词。它的返回值是迭代器,返回第一个满足条件(谓词返回true)的元素的迭代器。
4.2.2 二元谓词
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
class myCmp
{
public:
bool operator()(int v1,int v2)
{
return v1>v2;
}
};
void test01()
{
vector<int> v;
v.push_back(5);
v.push_back(2);
v.push_back(4);
v.push_back(3);
v.push_back(1);
//排序
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(),myCmp());
for(vector<int>::iterator it = v.begin();it!=v.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
运行结果:
4.3 内建函数对象
STL内建了一些函数对象(仿函数),包含头文件<functional>
之后即可直接使用。内建函数对象可以分为三类:
算术仿函数、关系仿函数、逻辑仿函数。
4.3.1 算术仿函数
功能:实现四则运算
仿函数原型如下,其中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 modules<T> // 取模仿函数
template<class T> T negate<T> // 取反仿函数
示例:
#include<iostream>
using namespace std;
#include<functional>
// negate
void test01()
{
negate<int> n;//创建函数对象
cout << n(50) << endl;
}
// plus
void test02()
{
plus<int> p;
// 虽然是需要传入两个数据,但是类型声明只需写一个即可,因为算是运算在同类型之间进行
cout << p(10,20) << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
执行结果:
4.3.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>
using namespace std;
#include<vector>
#include<functional> //包含头文件
#include<algorithm>
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(50);
v.push_back(30);
v.push_back(20);
v.push_back(40);
for(vector<int>::iterator it = v.begin();it!=v.end();it++)
{
cout << *it << " ";
}
cout << endl;
// 利用内建仿函数进行降序排列
sort(v.begin(),v.end(),greater<int>());
for(vector<int>::iterator it = v.begin();it!=v.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
4.3.3 逻辑仿函数
(用的不多)
函数原型:
template<class T> bool logic_and<T> // 逻辑与
template<class T> bool logic_or<T> // 逻辑或
template<class T> bool logic_not<T> // 逻辑非
示例:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void test01()
{
vector<bool> v;
v.push_back(true);
v.push_back(false);
v.push_back(true);
v.push_back(false);
for(vector<bool>::iterator it = v.begin();it!=v.end();it++)
{
cout << *it << " ";
}
cout << endl;
vector<bool> v2;
v2.resize(v.size());
// 必须先有空间,然后能用transform算法
transform(v.begin(),v.end(),v2.begin(),logical_not<bool>());
for(vector<bool>::iterator it = v2.begin();it!=v2.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
运行结果: