函数对象
10.STL-函数对象
10.1函数对象
10.1.1函数对象概念
概念:
- 重载函数调用操作符的类,其对象常称为函数对象
- 函数对象使用重载的()时,行为类似函数调用,也叫仿函数
本质:
函数对象(仿函数)是一个类,不是一个函数
10.1.2函数对象使用
特点:
- 函数对象在使用时,也可以像普通函数那样调用,可以有参数,可以有返回值
- 函数对象超出普通函数的概念,函数对象可以有自己的状态
- 函数对象可以作为参数传递
示例:
#include
using namespace std;
class MyAdd
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
//1.函数对象在使用时,可以像普通函数那样调用,可以有参数可以有返回值
void test01()
{
MyAdd m1;
cout << m1(10, 10) << endl;
}
class MyPrint
{
public:
MyPrint()
{
this->count = 0;
}
void operator()(string test)
{
cout << test << endl;
this->count++;
}
int count; //内部自己状态,统计使用次数
};
void test02()
{
MyPrint m2;
m2("Helloworld");
m2("Helloworld");
m2("Helloworld");
cout << "MyPrint调用的次数为:" << m2.count << endl;
}
//3.函数对象可以作为参数传递
void DoPrint(MyPrint &m,string str)
{
m(str);
}
void test03()
{
MyPrint m3;
DoPrint(m3, "Hello C++");
}
int main()
{
test01();
test02();
test03();
return 0;
}
running
20
Helloworld
Helloworld
Helloworld
MyPrint调用的次数为:3
Hello C++
总结
- 仿函数写法非常灵活,可以作为参数进行传递
10.2谓词
概念:
- 返回bool类型的仿函数称为谓词
- 如果operator()接受一个参数,那么叫做一元谓词
- 如果operator()接受两个参数,那么叫做二元谓词
10.2.1一元谓词
#include
using namespace std;
#include
//一元谓词
class GreaterFive
{
public:
bool operator()(int val) //谓词
{
return val > 5;
}
};
void test01()
{
vectorv1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
//查找容器中有无大于5的数字
vector::iterator it=find_if(v1.begin(), v1.end(), GreaterFive()); //GreaterFive() 匿名函数对象
if (it == v1.end())
{
cout << "未找到" << endl;
}
else
{
cout<<"找到了大于5的数字:"<<*it<
running
找到了大于5的数字:6
10.2.2二元谓词
#include
using namespace std;
#include
#include
//二元谓词
class MyCompare
{
public:
bool operator()(int v1, int v2) //二元谓词
{
return v1 > v2;//降序
}
};
void test01()
{
vectorv;
v.push_back(50);
v.push_back(60);
v.push_back(30);
v.push_back(15);
sort(v.begin(), v.end());
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " " << endl;
}
//使用函数对象改变算法策略,变为排序规则从大到小
sort(v.begin(), v.end(), MyCompare()); //MyCompare()匿名函数对象
cout << "--------------------------" << endl;
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " " << endl;
}
}
int main()
{
test01();
return 0;
system("pause");
}
running
15
30
50
60
--------------------------
60
50
30
15
10.3内建函数对象
10.3.1内建函数对象意义
概念:
- STL内建了一些函数对象
分类: - 算数仿函数
- 关系仿函数
- 逻辑仿函数
用法:
- 这些仿函数所产生的对象,用法和一般函数完全一样
- 使用内建函数对象,需要引入头文件
#include
10.3.2算数仿函数
功能描述:
- 实现四则运算
- 其中
negate
是一元运算,其他都是二元运算
仿函数原型:
template
加法仿函数T plus template
减法仿函数T minus template
乘法仿函数T multiplies template
除法仿函数T divides template
取模仿函数T modulus template
取反仿函数T negate
示例:
#include
using namespace std;
#include //内建函数对象头文件
//内建函数对象 算数仿函数
//negate 一元仿函数 取反仿函数
void test01()
{
negaten;
cout << n(50) << endl;;//对50进行取反操作
}
//plus 二元仿函数 加法
void test02()
{
plusp;
cout << p(10, 20) << endl; //加法运算
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
running
-50
30
10.3.3关系仿函数
功能描述:
- 实现关系对比
放函数原型:
template
等于bool equal_to template
不等于bool not_equal_to template
大于bool greater template
大于等于bool greater_equal template
小于bool less template
小于等于bool less_equal
示例:
#include
using namespace std;
#include
#include //算法的头文件
#include
//关系仿函数
class MyCompare
{
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
};
void test01()
{
vectorv;
v.push_back(3);
v.push_back(1);
v.push_back(4);
v.push_back(5);
v.push_back(2);
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//降序
cout << "-------------------" << endl;
//sort(v.begin(), v.end(), MyCompare());//MyCompare()自己创建的仿函数
sort(v.begin(), v.end(), greater());// 内建的仿函数需要头文件 #include
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
running
3 1 4 5 2
-------------------
5 4 3 2 1
10.3.4逻辑仿函数
功能描述:
- 实现逻辑运算
函数原型:
template
逻辑与bool logical_and template
逻辑或bool logical_or template
逻辑非bool logical_not
示例:
#include
using namespace std;
#include
#include
#include
//逻辑仿函数
void test01()
{
vectorv;
v.push_back(true);
v.push_back(false);
v.push_back(true);
v.push_back(false);
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//利用逻辑非,将容器取反 搬运到容器v2中
vectorv2;
v2.resize(v.size());//目标容器必须提前开辟好空间(指定大小),否则数据搬运不进去
//搬运 transform()
transform(v.begin(), v.end(), v2.begin(), logical_not());
for (vector::iterator it2 = v2.begin(); it2 != v2.end(); it2++)
{
cout << *it2 << " ";
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
running
1 0 1 0
0 1 0 1
总结:
- 逻辑仿函数实际应用较少,了解即可
综上整理于黑马程序员