概念
重载 函数调用操作符()的类,其对象常被称为 函数对象
函数对象使用重载的()时,行为类似函数调用,也叫仿函数
本质
函数对象(仿函数)是一个类,不是一个函数
特点:
一 函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
二 函数对象超出普通函数的概念,函数对象可以有自己的状态
三 函数对象可以作为参数传递
以上三点先脑子有个印象
函数对象基本使用
三种方法
一 函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
class MyAdd{
public://operator操作人员
int operator()(int v1,int v2){
return v1+v2;
}
};
//1 函数对象在使用时,可以像 普通函数 那样调用,可以有参数,可以有返回值
void test01(){
MyAdd myAdd;
cout<<myAdd(10,10)<<endl;
}
2 函数对象超出普通函数的概念,函数对象可以有自己的 状态(count)
这个代码要仔细琢磨
class MyPrint{
public:
MyPrint()//构造函数
{
this->count=0;
}
void operator()(string test){
cout<<test<<endl;
this->count++;//记录调用了多少次
}
int count;//内部自己的状态
// int this->count=0;//是错误的,所以需要上面的构造函数
};
void test02(){
MyPrint myprint;
myprint("hello world");
myprint("hello world as");
myprint("hello world");
myprint("hello world as");
cout<<"myPrint调用次数为多少"<<myprint.count<<endl;
}
3 函数对象可以作为参数传递
void doprint(MyPrint &mp,string test){
mp(test);
}
void test03(){
MyPrint myPrint;
doprint(myPrint,"hello c++");//myPrint是函数对象作为参数传递
// 然后还可以利用自身重载符号(),像一个函数一样调用 mp(test);
}
一元谓词
概念:返回bool类型的仿函数称为谓词
仿函数这么用
class GreateFive{
public:
bool operator()(int val){
return val>5;
}
};
完整代码
#include <iostream>
//概念:返回bool类型的仿函数称为谓词
using namespace std;
#include<vector>
#include<algorithm>
//如果operator()接受一个参数,那么叫做一元谓词
//如果operator()接受两个参数,那么叫做二元谓词
class GreateFive{
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的数字
// GreateFive()是匿名的函数对象
vector<int>::iterator it=find_if(v.begin(),v.end(),GreateFive()); //始 末 值
if(it==v.end()){
cout<<"未找到"<<endl;
}
else{
cout<<"找到了大于5的数字:"<<*it<<endl;//6 break直接退出
}
}
int main()
{
test01();
system("pause");
}
find_if()中三个值分别为 始 末 值
找到6立马break
二元谓词
如果operator()接受两个参数,那么叫做二元谓词
sort(v.begin(),v.end(),MyCompare()); //写入仿函数
仿函数实现代码
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
//概念:返回bool类型的仿函数称为谓词
//二元谓词
//如果operator()接受两个参数,那么叫做二元谓词
class MyCompare{
public:
bool operator()(int v1,int v2)//传两个参数进来
{
return v1>v2;
}
};
void test01(){
vector<int> v;
v.push_back(10);
v.push_back(30);
v.push_back(20);
v.push_back(50);
v.push_back(40);
sort(v.begin(),v.end()) ;
for(vector<int>::iterator it=v.begin();it!=v.end();it++){
cout<<*it<<" ";
}
cout<<endl;
//使用函数对象 改变算法策略,变为排序规则从小到大
// MyCompare是匿名的函数对象
sort(v.begin(),v.end(),MyCompare()); //写入仿函数
cout<<"-------------------------------"<<endl;
for(vector<int>::iterator it=v.begin();it!=v.end();it++){
cout<<*it<<" ";
}
cout<<endl;
}
int main()
{
test01();
system("pause");
}
内建函数
内建函数定义:封装好的仿函数直接拿来用
内建函数算数仿函数
功能描述:
实现四则运算
其中
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 modulus<T>
//
取模仿函数
template<class T> T negate<T>
//
取反仿函数
#include <iostream>
#include<functional>//内建的函数对象头文件
using namespace std;
//把封装好的仿函数直接拿来用
//实现四则运算
//其中negate是一元运算 取反仿函数,其他都是二元运算
void test01(){
negate<int> n;
cout<<n(50)<<endl;//-50
}
//用加法为例
//plus 二元仿函数 加法
void test02(){//只能整数相加
plus<int>p;
cout<<p(10,10)<<endl;
}
int main()
{
// test01();
test02();
system("pause");
}
内建函数关系仿函数
功能描述:
实现关系对比
仿函数原型:
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>
//
小于等于
用greater来做实例
#include <iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
//大于 greater 关系仿函数中最常用的
class MyCompare{
public:
bool operator()(int v1,int v2){
return v1>v2;
}
};
void test01(){
vector<int> v;
v.push_back(10);//无序序列
v.push_back(40);
v.push_back(30);
v.push_back(20);
v.push_back(50);
for(vector<int>::iterator it=v.begin();it!=v.end();it++){
cout<<*it<<" ";
}
cout<<endl;
//降序
// 自定义的 sort(v.begin(),v.end(),MyCompare());//默认升序
sort(v.begin(),v.end(),greater<int>());//此时 greater<int>()实现效果和 MyCompare()一样
for(vector<int>::iterator it=v.begin();it!=v.end();it++){
cout<<*it<<" ";
}
cout<<endl;
// cout<<greater<int>(20,100);用法错误
}
int main()
{
test01();
system("pause");
}
内建函数逻辑仿函数
功能描述:
实现逻辑运算
函数原型:
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;
//逻辑非 logical_not
void test01(){
vector<bool>v;
v.push_back(true);//1
v.push_back(false);//0
v.push_back(true);//1
v.push_back(false);//0
for(vector<bool>::iterator it=v.begin();it!=v.end();it++){
cout<<*it<<" ";
}
cout<<endl;
//利用逻辑非 将容器v搬运到容器v2中,并执行取反操作
vector<bool>v2;
v2.resize(v.size());//要搬运(transform)要提前开辟空间(resize)
transform(v.begin(),v.end(),v2.begin(),logical_not<bool>() );//原容器起始 原容器结束 目标容器起始 逻辑非
for(vector<bool>::iterator it=v2.begin();it!=v2.end();it++){
cout<<*it<<" ";//0 1 0 1
}
cout<<endl;
}
int main()
{
test01();
system("pause");
}