结构体冒泡排序年龄

#include
 using namespace std;
 //1、设计英雄的结构体
 struct Hero{
     string name;
     int age;
     string sex;
     
 };
 
 int main()
 {
     //2、创建数组进行排序
     struct Hero heroArray[5]=
     {
     {"刘备",23,"男"},
     {"关羽",22,"男"},
     {"张飞",20,"男"},
     {"赵云",21,"男"},
     {"貂蝉",19,"女"}
     };
     int len=sizeof(heroArray)/sizeof(heroArray[0]);
     cout<<"排序前:"<      for(int i=0;i          cout<<"姓名:"<      }
     
     
     //3、对数组进行排序,按照年龄进行升序排序,冒泡排序 
    for(int i=0;i          for(int j=0;j              if(heroArray[j].age              struct Hero temp=heroArray[j];
                 heroArray[j]=heroArray[j+1];
                 heroArray[j+1]=temp;
             }
         }
     }
     cout<<"排序后:"<      for(int i=0;i          cout<<"姓名:"<      }
     return 0;
 }

你可能感兴趣的:(算法,c++,数据结构)