C56-结构体1
2025-05-10 12:10:59
发布于:江苏
14阅读
0回复
0点赞
结构体的输入输出
注意student是一个类型,不是变量!
#include<bits/stdc++.h>
using namespace std;
//定义了一个结构体模板
struct student{ //自定义的类型
string name;
int age;
double score;
};
//cmp是自定义的比较函数,指定比较规则
bool cmp(student x, student y){
return x.score > y.score; //按照分数进行的排序从大到小的排序
}
int main(){
student a[100] = {
{"齐桓公小白", 48, 300},
{"晋文公重耳", 50, 299},
{"楚庄王熊旅", 30, 298},
{"吴国姬光", 59, 180},
{"越王勾践", 34, 181}
};
for (int i=0; i<5; ++i){
cout << a[i].name <<'\t';
cout << a[i].age <<'\t';
cout << a[i].score << endl;
}
sort(a, a+5, cmp); //cmp是自定义的比较函数, 结构体排序必须要写
cout << endl;
for (int i=0; i<5; ++i){
cout << a[i].name <<'\t';
cout << a[i].age <<'\t';
cout << a[i].score << endl;
}
return 0;
}
A7864.【结构体】输出信息
#include <bits/stdc++.h>
using namespace std;
struct node{
string name;
int x;
string id;
};
node a[10005];
int main(){
int n;
cin >> n;
for (int i=1; i<=n; i++){
cin >> a[i].name >> a[i].x >> a[i].id;
}
for (int i=1; i<=n; i++){
if (a[i].x == 1){
cout << a[i].name <<' '<< a[i].id << endl;
}
}
return 0;
}
这里空空如也
有帮助,赞一个