C语言的sort函数是一类用于数组排序的函数以下是其简单的使用:
1.头文件:
#include
2.使用命名空间:
using namespace std;
3.函数形式:
sort(数组名,数组名+元素个数,排序函数);
默认排序函数为升序,也可以自己写函数
4.简单使用:
(1)默认:
程序代码:
#include
#include
using namespace std; int main(){ const int n=6; int a[6]={5,12,7,2,9,3}; sort(a,a+n);//对数组a进行排序 for(int i=0;i
运行结果:

(2)自定义排序:
程序代码:
#include
#include
using namespace std; bool comp(int a,int b) { return a>b; } int main(){ const int n=6; int a[6]={5,12,7,2,9,3}; sort(a,a+n,comp);//对数组a进行排序 for(int i=0;i
运行结果:

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/222554.html原文链接:https://javaforall.net
