日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

C++结构体多级排序的三种方法

發(fā)布時(shí)間:2024/10/8 c/c++ 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++结构体多级排序的三种方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

C++結(jié)構(gòu)體多級(jí)排序的三種方法

struct node{int chinese,math;char name[15]; };

需求:按數(shù)學(xué)成績(jī)從大到小排序?

1.自定義比較器

//自定義比較函數(shù) bool cmp(node a,node b){return a.math>b.math; }

2.定義友元函數(shù)

struct node{int chinese,math;char name[15];//友元函數(shù)friend bool operator<(node a,node b){return a.math>b.math;} };

3.重載小于運(yùn)算符

struct node{int chinese,math;char name[15];//重載小于運(yùn)算符bool operator <(const node&b)const{return math>b.math;} };

完整實(shí)例程序

#include <iostream> #include <cstdio> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f const int maxn = (1e5+10);struct node{int chinese,math;char name[15];/*//重載小于運(yùn)算符bool operator <(const node&b)const{return math>b.math;}*///友元函數(shù)friend bool operator<(node a,node b){return a.math>b.math;} }; node arr[maxn]; //自定義比較函數(shù) bool cmp(node a,node b){return a.math>b.math; } int gcd(int a,int b) {return b==0?a:gcd(b,a%b); } int lcm(int a,int b) {return a/gcd(a,b)*b; } int main() {int n;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d%d%s",&arr[i].chinese,&arr[i].math,arr[i].name);}sort(arr,arr+n);printf("%d\n%s\n",lcm(arr[0].chinese,arr[1].chinese),arr[0].name); } /* 3 50 60 張三 40 80 李四 45 100 王五 */

?

總結(jié)

以上是生活随笔為你收集整理的C++结构体多级排序的三种方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。