通過(guò)c++的sort函數(shù)實(shí)現(xiàn)成績(jī)排序功能
sort函數(shù)用于C++中,對(duì)給定區(qū)間所有元素進(jìn)行排序,默認(rèn)為升序,也可進(jìn)行降序排序。sort函數(shù)進(jìn)行排序的時(shí)間復(fù)雜度為n*log2n,比冒泡之類(lèi)的排序算法效率要高,sort函數(shù)包含在頭文件為#include<algorithm>的c++標(biāo)準(zhǔn)庫(kù)中。
題目描述:
有N個(gè)學(xué)生的數(shù)據(jù),將學(xué)生數(shù)據(jù)按成績(jī)高低排序,如果成績(jī)相同則按姓名字符的字母排序,如果姓名的字母序也相同,則按照學(xué)生的年齡排序,并輸出N個(gè)學(xué)生排序后的信息。
#include<stdio.h> #include<algorithm> #include<string.h> using namespace std; struct E { char name[101]; int age; int score; }buf[1000]; bool cmp(E a, E b) { if (a.score != b.score) return a.score < b.score; int tmp = strcmp(a.name, b.name); if (tmp != 0) return tmp < 0; else return a.age < b.age; } int main() { int n; while (scanf_s("%d", &n) != EOF) { for (int i = 0; i < n; i++) { scanf_s("%s%d%d", buf[i].name,sizeof(buf[i].name), &buf[i].age, &buf[i].score); } sort(buf, buf + n, cmp); printf("\n"); for (int i = 0; i < n; i++) { printf("%s %d %d\n", buf[i].name, buf[i].age, buf[i].score); } } return 0; }
注意事項(xiàng)
scanf和scanf_s區(qū)別使用,scanf_s需要標(biāo)明緩沖區(qū)的大小,因而多出一個(gè)參數(shù)。 Unlike scanf and wscanf, scanf_s and wscanf_s require you to specify buffer sizes for some parameters. Specify the sizes for all c, C, s, S, or string control set [] parameters. The buffer size in characters is passed as an additional parameter. It immediately follows the pointer to the buffer or variable. For example, if you're reading a string, the buffer size for that string is passed as follows:
char s[10]; scanf_s("%9s", s, (unsigned)_countof(s)); // buffer size is 10, width specification is 9
結(jié)果
通過(guò)運(yùn)算符重載來(lái)實(shí)現(xiàn)
#include<stdio.h> #include<algorithm> #include<string.h> using namespace std; struct E { char name[101]; int age; int score; bool operator <(const E &b) const { if (score != b.score) return score < b.score; int tmp = strcmp(name, b.name); if (tmp != 0) return tmp < 0; else return age < b.age; } }buf[1000]; int main() { int n; while (scanf_s("%d", &n) != EOF) { for (int i = 0; i < n; i++) { scanf_s("%s%d%d", buf[i].name,sizeof(buf[i].name), &buf[i].age, &buf[i].score); } sort(buf, buf + n); printf("\n"); for (int i = 0; i < n; i++) { printf("%s %d %d\n", buf[i].name, buf[i].age, buf[i].score); } } return 0; }
由于已經(jīng)指明了結(jié)構(gòu)體的小于運(yùn)算符,計(jì)算機(jī)便知道了該結(jié)構(gòu)體的定序規(guī)則。 sort函數(shù)只利用小于運(yùn)算符來(lái)定序,小者在前。于是,我們?cè)谡{(diào)用sort時(shí)便不必特別指明排序規(guī)則(即不使用第三個(gè)參數(shù))。
總結(jié)
到此這篇關(guān)于通過(guò)c++的sort函數(shù)實(shí)現(xiàn)成績(jī)排序的文章就介紹到這了,更多相關(guān)c++ sort函數(shù)內(nèi)容請(qǐng)搜素腳本之家以前的文章或下面相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
歸并排序的遞歸實(shí)現(xiàn)與非遞歸實(shí)現(xiàn)代碼
以下是對(duì)歸并排序的遞歸實(shí)現(xiàn)與非遞歸實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下2013-08-08C語(yǔ)言實(shí)現(xiàn)自行車(chē)存放管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)自行車(chē)存放管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08Vs2019+Qt+Opencv環(huán)境配置心得(圖文)
這篇文章主要介紹了Vs2019+Qt+Opencv環(huán)境配置心得(圖文),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08C語(yǔ)言全面細(xì)致講解單雙精度f(wàn)loat與double的使用方法
C語(yǔ)言中小數(shù)的數(shù)據(jù)類(lèi)型為 float 或 double:float 稱(chēng)為單精度浮點(diǎn)數(shù),double 稱(chēng)為雙精度浮點(diǎn)數(shù)。不像整數(shù),小數(shù)的長(zhǎng)度始終是固定的,float 占用4個(gè)字節(jié),double 占用8個(gè)字節(jié)2022-05-05C語(yǔ)言實(shí)現(xiàn)帶頭雙向循環(huán)鏈表
本文主要介紹了C語(yǔ)言實(shí)現(xiàn)帶頭雙向循環(huán)鏈表,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03VC++實(shí)現(xiàn)添加文件關(guān)聯(lián)的方法示例
這篇文章主要介紹了VC++實(shí)現(xiàn)添加文件關(guān)聯(lián)的方法,涉及VC++針對(duì)注冊(cè)表的寫(xiě)入與VC事件響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2017-08-08C語(yǔ)言中設(shè)置進(jìn)程優(yōu)先順序的方法
這篇文章主要介紹了C語(yǔ)言中設(shè)置進(jìn)程優(yōu)先順序的方法,包括setpriority()函數(shù)和getpriority()函數(shù)以及nice()函數(shù),需要的朋友可以參考下2015-08-08