目录
1 函数对象
1. 基本概念
概念
-
①重载函数调用操作符的类,其对象常称为函数对象
-
②函数对象使用重载的()时,行为类似函数调用,也叫仿函数
本质
-
函数对象(仿函数)本身是一个类,并非一个函数
1.2 使用
特点
-
函数对象在使用时 ,可以像普通函数那样调用,可以有参数与返回值
-
函数对象超出普通函数的概念,可以有自己的状态
-
函数对象可以作为参数传递
测试
-
①仿函数
MyAdd
class MyAdd
{
public:
int operator()(int x,int y)
{
return x + y;
}
};
test
函数,直接调用普通函数那样调用
void test01()
{
MyAdd myadd;
cout << myadd(1, 5) << endl;
}
-
②仿函数
MyPrint
,内有属性count
来记录自己状态
class MyPrint
{
public:
MyPrint()
{
this->count = 0; // 初始化为0
}
void operator()(string s)
{
cout << s << endl;
this->count++;
}
int count; // 记录自己状态
};
test
函数,调用多次MyPrint
函数
void test02()
{
MyPrint myprint;
myprint("Jocye is my ba"); myprint("Jocye is my ba");
myprint("Jocye is my ba"); myprint("Jocye is my ba");
cout << myprint.count << endl;
}
-
③
doPrint
函数
void doPrint(MyPrint& mp,string s) // mp作为一个参数进行传递
{
mp(s); // 同时还能使用自身来打印s
}
test
函数,我们调用doPrint
函数
void test03()
{
MyPrint myprint;
doPrint(myprint, "Hello my");
}
2 谓词
2.1 基本概念
概念:返回bool
类型的仿函数称为谓词
-
如果
operator()
接受一个参数,称为一元谓词 -
如果
operator()
接受两个参数,称为二元谓词
2.2 一元谓词
void test04()
{
vector <int> v;
int i = 10;
while (i--)
{
v.push_back(i);
}
// 上面GreateFive仿函数,正常应该先创建一个对象,下面再使用
// 这里我直接使用匿名对象GreateFive();
vector<int>::iterator it = find_if(v.begin(), v.end(), GreateFive());
if (it != v.end())
cout << "有了:" << *it;
else
cout << "没有" << endl;
}
find_if
(起始迭代器,终止迭代器,仿函数); 返回的也是个迭代器,找到返回所在位置迭代器,否则返回end();
-
仿函数
GreateFive()
,大于5返回真
class GreateFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
由于是从9
加进去,加到1
,因此第一个数9
就满足,返回
2.3 二元谓词
void test05()
{
vector<int> v;
v.push_back(10); v.push_back(40); v.push_back(20);
v.push_back(30); v.push_back(50); v.push_back(60);
sort(v.begin(),v.end());
printv(v);
// 使用函数对象MyCompare()使其实现降序
sort(v.begin(), v.end(), MyCompare());
printv(v);
}
二元谓词MyCompare
class MyCompare
{
public:
bool operator()(int x, int y) // 二元谓词,2个参数
{
return x > y;
}
};
打印函数printv()
// 二元谓词
void printv(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
3 内建函数对象
3.1 基本概念
首先,STL
内建了一些函数对象(仿函数)
分类
-
算数仿函数
-
关系仿函数
-
逻辑仿函数
使用
-
这些仿函数所产生的对象,用法与一般函数完全相同
-
使用内建函数对象,需要引用头文件
#include<functional>
3.2 算数仿函数
功能:实现四则运算
函数
// 算数仿函数
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> 取反
其中negate
是一元运算,其他都是二元运算
测试
-
①取反仿函数
void test06()
{
negate<int> n;
cout << n(50) << endl;
}
-
②加法仿函数
void test07()
{
plus<int> p; // 默认传同种数据类型
cout << p(5,10) << endl;
}
3.3 关系仿函数
功能:实现关系对比
函数
// 关系仿函数
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> 小于等于
测试
在此之前,我们创建vector
容器里面存入数据,如果想让数据降序排序,需要自己实现仿函数
void test08()
{
vector<int> v;
v.push_back(10); v.push_back(40); v.push_back(20);
v.push_back(30); v.push_back(50); v.push_back(60);
sort(v.begin(), v.end());
printv(v);
// 使用函数对象MyCompare()使其实现降序
sort(v.begin(), v.end(), MyCompare());
printv(v);
}
此时,我们使用内建函数greater
// 使用内建函数对象
sort(v.begin(), v.end(), greater<int>());
而若是想比较其他的数据,如float
,我们只需要修改为greater<float>();
即可
同时,greater
是比较常用的
我们打开sort
排序源码
可以发现,如果要重载,上面有3
个参数,而下面默认的只有2
个参数(起始和终止区间)
而默认的2
个参数的,调用的是less<>
,因此默认使用升序排序从小到大
3.4 逻辑仿函数
功能:实现逻辑运算
函数
// 逻辑仿函数
template<class T> bool logical_and<T> 逻辑与,同时为真才真,其余为假
template<class T> bool logical_or<T> 逻辑或,同时为假才假,其余为真
template<class T> bool logical_not<T> 逻辑非,取反操作
测试
-
创建容器
v
和容器v2
,v
中存入true
与false
的数据,取反后存入v2
void test09()
{
vector<int > v;
v.push_back(true); v.push_back(false); v.push_back(true);
v.push_back(true); v.push_back(false);
printv(v);
// 取反,放入另一个容器
vector<int> v2;
v2.resize(v.size()); // 指定v2的size为v的size
transform(v.begin(), v.end(), v2.begin(),logical_not<int>());
//搬运算法 起始 终止 目标的起始 逻辑非
printv(v2);
}