#include <iostream>#include <string>using namespace std;struct Hero { string name; int age; string sex;};void bubbleSort(struct Hero hero[], int len);void printHero(struct Hero hero[], int len);int main() { // 1.设计英雄结构体 // 2.创建数组存放5名英雄 struct Hero hero[5] = { { "刘备", 23, "男" }, { "关羽", 22, "男" }, { "张飞", 20, "男" }, { "赵云", 21, "男" }, { "貂蝉", 19, "女" } }; int len = sizeof(hero) / sizeof(hero[0]); for (int i = 0; i < len; i++) { cout << hero[i].name << ","; cout << hero[i].age << ","; cout << hero[i].sex; cout << endl; } // 3.对数组进行排序,按照年龄进行升序排序 bubbleSort(hero, len); // 4.讲排序后的结果打印输出 printHero(hero, len); return 0;}void bubbleSort(struct Hero hero[], int len) { for (int i = 0; i < len; i++) { for (int j = 0; j < len - i - 1; j++) { // 如果j下标的元素年龄大于j+1下标的元素年龄,交换两个元素 if (hero[j].age > hero[j + 1].age) { struct Hero temp = hero[j]; hero[j] = hero[j + 1]; hero[j + 1] = temp; } } }}void printHero(struct Hero hero[], int len) { cout << "-" << endl; for (int i = 0; i < len; i++) { cout << hero[i].name << ","; cout << hero[i].age << ","; cout << hero[i].sex; cout << endl; }}