已学会的sort用法
头文件: #include < algorithm >
时间复杂度:n*log2n
用法:
//默认从小到大排序
sort(a,a+n);//数组(要排序部分的首地址,要排序部分的末地址)
sort(v.begin(),v.end());//STL标准容器中的vector、string、deque(还不会)
特别的从大到小排序需加入头文件 #include< functional >
sort(p,p+n,greater<int>());
对结构体的排序:
#include<bits/stdc++.h>
using namespace std;
struct A
{
int x,y;
};
A a[100];
bool cmp(A a,A b)
{
if(a.x!=b.x)
return a.x<b.x;
return a.y<b.y;
}
int main()
{
int i,n=5;
for(i=0;i<n;i++)
scanf("%d %d",&a[i].x,&a[i].y);
sort(a,a+5,cmp);
for( i=0;i<5;i++)
printf("%d %d\n",a[i].x,a[i].y);
}