<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选出的是个位数最小的数字;