1、排序方法:
sort(数组起始指针,数组尾指针,排序规则);
数组起始指针,数组尾指针是左闭右开
排序规则可以省略,也可以用系统的,也可以自己写
2、例子:
int a[]={9,2,4,5,10,7,30};
sort(a,a+7);
这是默认的对数组从小到大排列
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
//结构体排序一
//按姓名从小到大排序,姓名一样,按年龄从小到大排序
struct student{
string name;//姓名
int age;//年龄
};
int comp(const student &s1,const student &s2){//自己定义的排序规则
if(s1.name==s2.name){
return s1.age<s2.age;
}
else{
return s1.name<s2.name;
}
}
//结构体排序二
//按姓名从小到大排序,姓名一样,按年龄从小到大排序
struct student2{
string name;//姓名
int age;//年龄
bool operator < (const student2 & s2) cons