#include <iostream> #include <algorithm> #include <stdlib.h> using namespace std; struct node { int x; int y; }; int cmp(const void *a,const void *b) { return *(int *)a-*(int *)b; }//从小到大排 bool comp(int a,int b) { if(a<b) return true; return false; }//从小到大排 bool cmpp(node a,node b) { if(a.x<b.x) return true; else { if(a.x==b.x) { if(a.y<b.y) return true; } return false; } return false; }//先按x从小到大排,如果x相等,再按y从小到大排 int main() { int a[5]={5,4,3,2,1}; int aa[5]={5,4,3,2,1}; int b[5]={10,9,8,7,6}; node s[3]={{1,2},{2,4},{2,3}}; int i; sort(a,a+5); sort(aa,aa+5,comp); sort(s,s+3,cmpp); qsort(b,5,4,cmp); for(i=0;i<=4;i++) cout<<a[i]<<" "; cout<<endl; for(i=0;i<=4;i++) cout<<aa[i]<<" "; cout<<endl; for(i=0;i<=4;i++) cout<<b[i]<<" "; cout<<endl; for(i=0;i<=2;i++) cout<<s[i].x<<" "<<s[i].y<<endl; }
运行截图: