[1] 一个基本的函数对象的使用 三个例子
#include<iostream>
#include<vector>
using namespace std;
void ShowMessage()
{
cout<<"Hello, Nice to meet you~"<<endl;
}
class MyShowMessage
{
public:
void operator()()
{
cout<<"Hello, Nice to meet you~"<<endl;
}
};
int main()
{
//函数对象与自定义排序
ShowMessage();
MyShowMessage m;//m叫做函数对象
m();//调用函数对象的成员函数
(MyShowMessage())();//以上可以简写为这样
return 0;
}
另外一个例子(重载括号)
#include<iostream>
#include<vector>
using namespace std;
int AddTen(int n)
{
return n+10;
}
class MyAddTen
{
public:
int operator()(int n)
{
return n+10;
}
};
int main()
{
int a=AddTen(90);
MyAddTen m;
a=m(990);
a=((MyAddTen()))(9990);//使用临时对象
return 0;
}
再看一个具体的例子:若想要实现和下面这个同样的功能:
#include<iostream>
#include<vector>
using namespace std;
bool GreaterThan(int a, int b)
{
return a>b;
}
class MyGreaterThan
{
public:
bool operator()(int a, int b)
{
return a>b;
}
};
int main()
{
cout<<GreaterThan(1,9)<<endl;
MyGreaterThan m;
cout<<m(10,9)<<endl;
cout<<((MyGreaterThan()))(10,9)<<endl;
return 0;
}
[2] functional 头文件中已经存在的可以用的函数对象:greater和less
#include<iostream>
#include<vector>
#include<functional>
//很多函数对象已经帮我写好了!greater接收两个数 less也一样
using namespace std;
int main()
{
greater<int> g;
cout<<g(10,9)<<endl;
cout<<((greater<int>()))(10,90)<<endl;
greater<double> g1;
cout<<g1(10.2,9.4)<<endl;
cout<<((greater<double>()))(10.2,90.9)<<endl;
return 0;
}
[3] 自定义排序 sort 方法一:直接用函数
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>//涉及算法,我们这里直接把sort拿来排序
using namespace std;
struct Point
{
int x;
int y;
int z;
};
bool LessThanPoint(Point a, Point b)
{
return sqrt(a.x*a.x+a.y*a.y+a.z*a.z)<sqrt(b.x*b.x+b.y*b.y+b.z*b.z);
}
void PrintPoint(Point a)
{
printf("Point: x=%05d, y=%05d, z=%05d.",a.x,a.y,a.z);
printf("The distance is:%6.4lf\n",sqrt(a.x*a.x+a.y*a.y+a.z*a.z));
}//方便输出而写的代码
int main()
{
Point v[]=
{
{3,4,5},
{-3,23,9},
{1,1,1},
{2,2,2},
{3,3,3},
{3,4,5},
{5,5,5},
{6,6,6},
{7,7,7},
{8,8,8},
};
sort(v,v+10,LessThanPoint);//按照自定义的规则,排序
return 0;
}
[4] 自定义排序 sort 方法二:使用函数对象的自定义排序 [代码有误..]
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>//涉及算法,我们这里直接把sort拿来排序
using namespace std;
struct Point
{
int x;
int y;
int z;
};
class MyLessThanPoint
{
public:
bool operator()(Point a, Point b)
{
return sqrt(a.x*a.x+a.y*a.y+a.z*a.z)<sqrt(b.x*b.x+b.y*b.y+b.z*b.z);
}
};
void PrintPoint(Point a)
{
printf("Point: x=%05d, y=%05d, z=%05d.",a.x,a.y,a.z);
printf("The distance is:%6.4lf\n",sqrt(a.x*a.x+a.y*a.y+a.z*a.z));
}//方便输出而写的代码
int main()
{
Point v[]=
{
{3,4,5},
{-3,23,9},
{1,1,1},
{2,2,2},
{3,3,3},
{3,4,5},
{5,5,5},
{6,6,6},
{7,7,7},
{8,8,8},
};
/*MyLessThanPoint m;
sort(v,v+10,m);*/
//按照函数对象的规则,排序,可以简写成下面一行语句:
sort(v,v+10,MyLessThanPoint());
for(int i=0;i<10;i++)
{
PrintPoint(v[i]);
}
return 0;
}
[5] 自定义排序 sort 方法三:直接重载小于号!对于vector也适用 [代码有误..]
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>//涉及算法,我们这里直接把sort拿来排序
using namespace std;
struct Point
{
int x;
int y;
int z;
bool operator <(const Point &a)
{
return sqrt(this->x*this->x+this->y*this->y+this->z*this->z)<sqrt(a.x*a.x+a.y*a.y+a.z*a.z);
}
};
void PrintPoint(Point a)
{
printf("Point: x=%05d, y=%05d, z=%05d.",a.x,a.y,a.z);
printf("The distance is:%6.4lf\n",sqrt(a.x*a.x+a.y*a.y+a.z*a.z));
}//方便输出而写的代码
int main()
{
Point v[]=
{
{3,4,5},
{-3,23,9},
{1,1,1},
{2,2,2},
{3,3,3},
{3,4,5},
{5,5,5},
{6,6,6},
{7,7,7},
{8,8,8},
};
sort(v,v+10);//已经有了小于关系,所以就不用再进行排序了
for(int i=0;i<10;i++)
{
PrintPoint(v[i]);
}
return 0;
}
[6] 简单应用&总结:从大到小的排序 自定义或者greater(记得头文件functional)
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>//涉及算法,我们这里直接把sort拿来排序
#include<functional>
using namespace std;
bool GreaterThan(int a, int b)
{
return a>b;
}
int main()
{
int v[]={23,13,43,564,-4,5,-23,44,55,13,-33};
sort(v,v+11,greater<int>());
for(int i=0;i<11;i++)
{
cout<<v[i]<<" ";
}
return 0;
}
[7] 考题运用:将学生的成绩从高到低排序,成绩相同按某科成绩排序,成绩均相同,按照ID排序
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>//涉及算法,我们这里直接把sort拿来排序
#include<functional>
using namespace std;
struct Student
{
int ID;
int Math;
int English;
};
bool MyCmp(Student x, Student y)
{
if((x.Math+x.English)!=(y.Math+y.English))
{
return (x.Math+x.English)>(y.Math+y.English);
}
else if(x.Math!=y.Math)
{
return (x.Math)>(y.Math);
}
else
{
return x.ID<y.ID;
}
}
void PrintStudent(Student s)
{
printf("Student:Total %7d Math %3d ID %2d\n",s.English+s.Math,s.Math,s.ID);
}
int main()
{
Student v[]=
{{90,45,21},{89,55,88},{99,10,33},{21,23,97},{4,20,44},{8,50,14},{70,45,21},{9,80,87},{33,20,90}
};
sort(v,v+9,MyCmp);
for(int i=0;i<9;i++)
{
PrintStudent(v[i]);
}
return 0;
}