#include<iostream>
#include<algorithm>
using namespace std;
struct Rule1{
bool operator()(const int &a1, const int &a2){
return a1 >a2; //从大到小排序
}
};
struct Rule2{
bool operator()(const int &a1, const int &a2){
return a1 % 10 < a2 % 10; //按个位数从小到大排序
}
};
void Print(int a[], int size){
for (int i = 0; i < size; i++)
cout << a[i] << " ";
cout << endl;
}
int main(){
int a[] = { 12, 45, 3, 98, 21, 7 }; //列表初始化数组,
//只有在定义数组时才能使用初始化和数组直接赋值给另一个数组
int num = sizeof(a) / sizeof(int); //求数组的长度
sort(a,a+num);
cout << "1) ";
Print(a, num);
sort(a, a + num,Rule1());
cout << "2) ";
Print(a, num);
sort(a, a + num, Rule2());
cout << "3) ";
Print(a, num);
cin.get();
return 0;
}
struct 结构名
{
bool opreator()(const T & a,const T & b){
return a1>a2; //由大到小
}
};