通過c++的sort函數(shù)實(shí)現(xiàn)成績排序功能
sort函數(shù)用于C++中,對給定區(qū)間所有元素進(jìn)行排序,默認(rèn)為升序,也可進(jìn)行降序排序。sort函數(shù)進(jìn)行排序的時(shí)間復(fù)雜度為n*log2n,比冒泡之類的排序算法效率要高,sort函數(shù)包含在頭文件為#include<algorithm>的c++標(biāo)準(zhǔn)庫中。
題目描述:
有N個(gè)學(xué)生的數(shù)據(jù),將學(xué)生數(shù)據(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é)果

通過運(yùn)算符重載來實(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)算符來定序,小者在前。于是,我們在調(diào)用sort時(shí)便不必特別指明排序規(guī)則(即不使用第三個(gè)參數(shù))。
總結(jié)
到此這篇關(guān)于通過c++的sort函數(shù)實(shí)現(xiàn)成績排序的文章就介紹到這了,更多相關(guān)c++ sort函數(shù)內(nèi)容請搜素腳本之家以前的文章或下面相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
C語言實(shí)現(xiàn)在windows服務(wù)中新建進(jìn)程的方法
這篇文章主要介紹了C語言實(shí)現(xiàn)在windows服務(wù)中新建進(jìn)程的方法,涉及C語言進(jìn)程操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06
深度解析三個(gè)常見的C語言內(nèi)存函數(shù)
這篇文章主要深度解析了三個(gè)常見的C語言內(nèi)存函數(shù)memcpy,memmove,memcmp,所以本文將對memcpy,memmove,memcmp 三個(gè)函數(shù)進(jìn)行詳解和模擬實(shí)現(xiàn),需要的朋友可以參考下2023-07-07
VC使用TerminateProcess結(jié)束進(jìn)程實(shí)例
這篇文章主要介紹了VC使用TerminateProcess結(jié)束進(jìn)程的方法,實(shí)例演示了TerminateProcess結(jié)束進(jìn)程的具體實(shí)現(xiàn)過程,在進(jìn)行VC應(yīng)用程序開發(fā)時(shí)非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
QT實(shí)現(xiàn)QML側(cè)邊導(dǎo)航欄的最簡方法
本文主要介紹了QT實(shí)現(xiàn)QML側(cè)邊導(dǎo)航欄的最簡方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
C++11中的可變參數(shù)模板/lambda表達(dá)式
C++11的新特性可變參數(shù)模板能夠讓我們創(chuàng)建可以接受可變參數(shù)的函數(shù)模板和類模板,相比C++98和C++03,類模板和函數(shù)模板中只能含固定數(shù)量的模板參數(shù),可變參數(shù)模板無疑是一個(gè)巨大的改進(jìn),這篇文章主要介紹了C++11中的可變參數(shù)模板/lambda表達(dá)式,需要的朋友可以參考下2023-03-03

