sort函数用于C++中,对给定区间所有元素进行排序,默认为升序,也可进行降序排序。sort函数进行排序的时间复杂度为n*log2n,比冒泡之类的排序算法效率要高,sort函数包含在头文件为#include的c++标准库中。
题目描述:
有N个学生的数据,将学生数据按成绩高低排序,如果成绩相同则按姓名字符的字母排序,如果姓名的字母序也相同,则按照学生的年龄排序,并输出N个学生排序后的信息。
#include
#include
#include
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