结构体排序

结构体排序:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

typedef struct student {
	string name;
	int achievement;
}student;

//< 升序   > 降序
bool com(student a, student b) {
	return a.achievement > b.achievement;
}

void show(student stu[], int n) {
	for (int i = 0; i < n; i++) {
		cout << "姓名: " << stu[i].name << " 成绩: " << stu[i].achievement << endl;
	}

}
int main() {
	student stu[] = { {"a", 13}, {"b", 19}, {"c", 11} };
	cout << "排序前" << endl;
	
	show(stu, 3);


	sort(stu, stu + 3, com);
	cout << "排序后" << endl;
	show(stu, 3);
}

posted @ 2021-09-07 17:05  JK~  阅读(33)  评论(0编辑  收藏  举报