函数对象的学习

<span style="font-size:24px;">#include<iostream>  
#include<vector>  
#include<string>  
#include<algorithm>  
#include<numeric>  
using  namespace std;  
class MyMax  
{  
public:  
    bool operator()(int a1,int a2)  
    {   
      if((a1%10)<(a2%10))  
         return 1;  
      else   
         return 0;  
    }  
};  
bool MyLess(int a1,int a2)  
{  
  if((a1%10)<(a2%10))  
         return 0;  
  else   
         return 1;  
}  
template<class T2,class T3>  
T2 Mycompare(T2 first,T2 last,T3 t3)  
{  
  T2 max=first;  
  for(;first!=last;first++)  
  {  
     if(t3(*max,*first))  
     {  
         *max=*first;  
     }  
  }  
  return max;  
}  
int main()  
{   
    int a[]={35,7,13,19,61};  
    cout<<*Mycompare(a,a+5,MyMax())<<endl;  
    cout<<*Mycompare(a,a+5,MyLess)<<endl;  
    system("pause");  
    return 0;  
}  </span>

基于函数对象的C++编程
利用编写Mycompare类模板,以函数MyLess和对象MyMax为一个对象参数传入T2、T3中,进行不同种类的排序,选出符合各自逻辑的最值。

其中:Myless选出的是个位数最大的数字;

     MyMax选出的是个位数最小的数字;

     大笑生气奋斗

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值