仿函数总结

概念 


重载 函数调用操作符()类,其对象常被称为 函数对象
函数对象使用重载的()时,行为类似函数调用,也叫仿函数 

本质
函数对象(仿函数)是一个类,不是一个函数 

特点:
一  函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
二  函数对象超出普通函数的概念,函数对象可以有自己的状态
三  函数对象可以作为参数传递 

 以上三点先脑子有个印象

函数对象基本使用

三种方法

一  函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值

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");
}

  • 26
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
排序自定义类型可以通过重载比较操作符或者使用仿函数来实现。重载比较操作符是一种简单的方法,可以通过在自定义类型中定义<操作符来指定排序规则。比如,如果要按照学生的成绩从高到低进行排序,可以在学生类中重载<操作符,然后在sort函数中传入自定义类型的比较函数。 另一种方法是使用仿函数,也就是重载函数调用操作符()的类。这个类可以接受两个自定义类型的参数,并根据自定义的比较规则返回比较结果。比如,可以创建一个名为StudentComparator的仿函数类来实现根据学生成绩排序的比较规则。然后在sort函数中传入这个仿函数类的实例作为比较函数。 使用仿函数的好处是可以更灵活地定义排序规则,可以根据不同的排序需求创建不同的仿函数类。同时,使用仿函数还可以方便地将排序规则封装起来,使代码更加清晰易读。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [sort对类对象进行自定义排序/重载操作符/友元函数/仿函数](https://blog.csdn.net/qq_43791377/article/details/105552632)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [C++ sort排序之降序、升序使用总结](https://download.csdn.net/download/weixin_38640473/14839215)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值