头文件为#include<algorithm>
sort函数包含在的c++标准库中,调用标准库里的排序方法可以实现对数据的排序。
三个参数:
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
第一个参数first:是要排序的数组的起始地址。
第二个参数last:是结束的地址
第三个参数comp是排序的方法,如果第三个参数不写,则默认的排序方法是从小到大排序。
例:
#include<iostream>
#include<algorithm>
using namespace std;
//自定义函数
bool comp(int a,int b){
return a>b;
}
int main(){
//sort函数第三个参数自己定义,实现从大到小
int a[]={1,3,4,2,9,8,6,7,5,10};
sort(a,a+10,comp);
for(int i=0;i<10;i++)
cout<<a[i]<<" ";
return 0;
}