通信录包含 “姓名”(最多20个字符)、 “生日”(包括“年”、“月”、“日”)、 “电话号码”、 “家庭地址”(最多50个字符)。 定义一个嵌套的结构类型,输入n(n<10)个学生信息,再按照他们的年龄从小到大的顺序输出,用c++实现
时间: 2023-12-18 13:29:22 浏览: 59
```cpp
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct Date {
int year;
int month;
int day;
};
struct Student {
string name;
Date birthday;
string phone;
string address;
};
bool cmp(Student a, Student b) {
if (a.birthday.year != b.birthday.year) {
return a.birthday.year < b.birthday.year;
} else if (a.birthday.month != b.birthday.month) {
return a.birthday.month < b.birthday.month;
} else {
return a.birthday.day < b.birthday.day;
}
}
int main() {
int n;
cin >> n;
Student students[n];
for (int i = 0; i < n; i++) {
cin >> students[i].name >> students[i].birthday.year >> students[i].birthday.month >> students[i].birthday.day >> students[i].phone >> students[i].address;
}
sort(students, students + n, cmp);
for (int i = 0; i < n; i++) {
cout << students[i].name << " " << students[i].birthday.year << " " << students[i].birthday.month << " " << students[i].birthday.day << " " << students[i].phone << " " << students[i].address << endl;
}
return 0;
}
```
--相关问题--:
1. 如何在C++中对结构体进行排序?
2. 如何在C++
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)