sort函数默认的排序方式是升序排序,即从小到大

对简单的数组排序

简单来说就是sort(begin,end,cmp);
sort函数中参数有三个(第三个可以省略)
其中begin是排序数组的起始地址
end是排序数组的结束地址(最后一位要排序元素的地址)这两个参数都是地址。

#include <iostream> #include<algorithm> using namespace std;  int main() { int a[6]={6,2,3,1,5,10}; sort(a,a+6);  for(int i=0;i<6;i++)
  cout<<a[i]<<" "; return 0;
}

对于降序排序可以用sort(a,a+10,greater());
也可以自定义cmp函数

bool cmp(int a,int b) {  return a>b;
}

另外相对应的升序排序用sort(a,a+10,less());

bool cmp(int a,int b) { return a<b;
}