自定义sort升序降序的方法如下

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
struct period
{
    int st;
    int end;
};
int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
bool cmp(period x,period y)          //升序排列
{
    return x.end<y.end;
}
int main()
{
    int a;
    int temp,ans=1;
    a=read();
    period n[a];
    for(int i=0;i<a;i++)
    {
        n[i].st=read();
        n[i].end=read();
    }
    sort(n,n+a,cmp);
    temp=n[0].end;
    for(int i=1;i<a;i++)
        if(n[i].st>=temp)
        {
            temp=n[i].end;
            ans++;
        }
    cout<<ans;
    return 0;
}
sort