标签:ret tor 共同点 std const operator div bit c++
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 1010; 5 struct node{ 6 int x,y; 7 friend bool operator<(const node &a,const node &b){//结构体内部定义排序规则 8 if(a.x != b.x) return a.x < b.x;//从小到大 9 else return a.y > b.y;//从大到小 10 } 11 }a[maxn]; 12 13 int main() 14 { 15 int n = 3; 16 a[0].x = 2; 17 a[0].y = 3; 18 a[1].x = 4; 19 a[1].y = 3; 20 a[2].x = 2; 21 a[2].y = 4; 22 23 sort(a,a+n); 24 for(int i=0;i<n;i++) 25 printf("x = %d,y = %d\n",a[i].x,a[i].y); 26 }
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 1010; 5 struct node{ 6 int x,y; 7 8 }a[maxn]; 9 10 int cmp(node &a, node &b){ 11 if(a.x != b.x) return a.x < b.x;//从小到大 12 return b.y < a.y;//从大到小 13 } 14 int main() 15 { 16 int n = 3; 17 a[0].x = 2; 18 a[0].y = 3; 19 a[1].x = 4; 20 a[1].y = 3; 21 a[2].x = 2; 22 a[2].y = 4; 23 24 sort(a,a+n, cmp); 25 for(int i=0;i<n;i++) 26 printf("x = %d,y = %d\n",a[i].x,a[i].y); 27 }
结构体的排序的两种方式,(1)在结构体内部定义排序规则:friend bool operator<(const node &a,const node &b),(2)在sort函数中放入比较函数cmp;两者的共同点在于return a.x < b.x即要求按x值从小到大排序
标签:ret tor 共同点 std const operator div bit c++
原文地址:https://www.cnblogs.com/0424lrn/p/12213013.html