#include
#include
using namespace std;
struct hero {
string name;
int age;
string sex;
};
//对结构体进行冒泡排序的函数
void bubbleSort(struct hero heroArray[], int len)
{
for (int i = 0; i < len-1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (heroArray[j].age > heroArray[j+1].age)
{
struct hero temp;
temp = heroArray[j];
heroArray[j] = heroArray[j + 1];
heroArray[j + 1] = temp;
}
}
}
}
//输出函数
void printInfo(struct hero heroArray[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "姓名" << heroArray[i].name << "年龄" << heroArray[i].age
<< "性别" << heroArray[i].sex << endl;
}
}
int main()
{
/// 创建数组 存放数组
struct hero heroArray[5] = {
{"刘备",23,"男"},
{"张飞",22,"男"},
{"关羽",19,"男"},
{"张飞",20,"男"},
{"貂蝉",21,"男"},
};
//计算数组长度
int len = sizeof(heroArray) / sizeof(heroArray[0]);
// 对年龄运用冒泡排序进行升序排列
bubbleSort(heroArray, len);
//输入排序后的结果
printInfo(heroArray, len);
return 0;
}