优化下列代码 #include<iostream> #include<bits/stdc++.h> using namespace std; struct Student { string name; int scroe; }a[7]; bool compare (Student.scroe,Student.scroe) { return a.scroe>b.scroe; } int main() { int m; cin>>m; for(int i=0;i<m;i++) { cin>>a[i].name; } for(int i=0;i<m;i++) { cin>>a[i].scroe; } sort(a[0].scroe,a[m-1].score,compare); for(int i=0;i<m;i++) { cout<<a[i].name<<" "; } }
时间: 2023-07-31 15:09:42 浏览: 67
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Student {
string name;
int score;
};
bool compare(const Student& a, const Student& b) {
return a.score > b.score;
}
int main() {
int m;
cin >> m;
vector<Student> students(m);
for (int i = 0; i < m; i++) {
cin >> students[i].name;
}
for (int i = 0; i < m; i++) {
cin >> students[i].score;
}
sort(students.begin(), students.end(), compare);
for (int i = 0; i < m; i++) {
cout << students[i].name << " ";
}
return 0;
}
相关问题
#include<iostream>#include <cmath>#include<bits/stdc++.h>
这是一个C++的程序,包含了iostream、cmath和bits/stdc++.h三个头文件。iostream头文件主要包含了输入输出流的内容,cmath头文件主要包含了一些数学函数,bits/stdc++.h头文件则包含了所有STL库。这个程序可能只是一个头文件的引用,并没有实际的代码实现。需要更多上下文信息以确定程序的用途。
# include <bits/stdc++.h>和# include <iostream>的区别
`#include <bits/stdc++.h>` 是一个非标准化的头文件,它包含了所有标准 C++ 库的头文件。因此,当使用这个头文件时,不需要单独包含其他头文件,就可以使用常用的 C++ 库函数和对象。
而 `#include <iostream>` 是标准 C++ 库的头文件之一,它包含了输入输出流的定义,如 `cout` 和 `cin`。这个头文件只包含输入输出流相关的内容,如果需要使用其他函数和对象,则需要包含其他相应的头文件。
因为 `#include <bits/stdc++.h>` 不是标准化的头文件,所以在编写一些需要跨平台的代码时,最好使用标准库头文件来确保代码的可移植性。