结构体案例二:英雄

#include<iostream>
using namespace std;
#include<string>
//创建结构体
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 - 1 - i; j++) {
			if (HeroArray[j].age > HeroArray[j+1].age) {//数组要传入具体夫人变量
			struct	Hero  temp = HeroArray[j];
			HeroArray[j] = HeroArray[j + 1];
			HeroArray[j + 1] = temp;
			
			}

		}
	}
 }
void printsort(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() {

	//创建数组存放5名英雄
	struct Hero HeroArray[3] = { {"刘备",25,"男"},{"张飞",20,"男"},{"关羽",34,"男"}, };
	//计算数组的长度
	 int len = sizeof(HeroArray) / sizeof(HeroArray[0]);
	 //测试输出
	 for (int i = 0; i < len; i++) {
		 cout <<"姓名: "<< HeroArray[i].name << "年龄: "<<HeroArray[i].age<<"性别: "<<HeroArray[i].sex<<endl;
	 }
	 bubbleSort(HeroArray, len);//调用函数,打印输出
	 printsort(HeroArray, len);
	 return 0;
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页