前言:
C語言的sort函數是一類用于數組排序的函數以下是其簡單的使用:
1.頭文件:
#include<algorithm>
2.使用命名空間:
using namespace std;
3.函數形式:
sort(數組名,數組名+元素個數,排序函數);
默認排序函數為升序,也可以自己寫函數
降序:
int cmp(student a,student b)
{return a.score>b.score;
}
?4.實例
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
int n;
struct student
{int score;char name[20];}stu[100];
int cmp(student a,student b)
{return a.score>b.score;
}
int main()
{cin>>n;for(int i=1;i<=n;i++){cin>>stu[i].score>>stu[i].name;}sort(stu+1,stu+1+n,cmp);cout<<stu[1].name;return 0;
}
結尾:
歡迎交流!