一年没写代码,复习各种函数的使用。
sort函数需要引用algorithm库,位于命名空间std内(std::sort),包含3个参数(第三个参数如不填写,将默认为升序排序)
例如sort(a+x,a+y,cmp) 就是数组(或是vector之类的容器)的第x+1至第y个的数据进行排序(以0为计数起点),排序原则按照bool函数cmp的返回值
bool cmp(const int a,const int b)
{
if(a>=b) return true;
else if (a<b) return false;
}
此时sort函数将会是升序排序,以此类推,可以通过cmp函数实现一些特殊的要求。
实验函数
#include<algorithm>
#include<cstdio>
#include<cstdlib>
using namespace std;
bool cmp(const int a,const int b)
{
if(a>b) return true;
else if(a<b) return false;
}
int main(void)
{
int a[800];
freopen("in.txt","