要引入#include<algorithm>
sort(begin,end,fangfa)
//参数分别为:起始地址,结束地址,升序or降序
//第三个不写默认为升序,从小到大sort(a,a+10)
降序有些特殊,要写一个bool型函数
#include<iostream>
#include<algorithm>
using namespace std;
bool complare(int a,int b)
{
return a>b;//降序
}
int main()
{
int a[10]={9,6,3,8,5,2,7,4,1,0};
for(int i=0;i<10;i++)
cout<<a[i]<<endl;
sort(a,a+10,complare);//在这里就不需要对complare函数传入参数了,这是固定的用法
//a[]是数组,单写a则为a[]数组的首地址,a+10即为首地址+10
for(int i=0;i<10;i++)
cout<<a[i]<<endl;
return 0;
}
注:a,a+10 用于一般数组,静态的
a.begin() a.begin()+10用于vector数组
其他al