#include
using namespace std;
#include
struct hero {
string name;
int age;
string sex;
};
void bubblesort(struct hero heroarray[4], int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j, len - i - i; j++) {
if (heroarray[j].age < heroarray[j + 1].age) {
hero temp = heroarray[j];
heroarray[j] = heroarray[j + 1];
heroarray[j + 1] = temp;
}
}
}
}
void printinfo(struct hero heroarray[4], 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[4]={
{"张飞",25,"男"},
{"赵云",28,"男"},
{"貂蝉",18,"女"},
{"吕布",35,"男"}
};
int len = sizeof(heroarray) / sizeof(heroarray[0]);
bubblesort(heroarray, len);
printinfo(heroarray, len);
system("pause");
return 0;
}
这是我写的一段关于输出英雄的代码,程序也没有报错,不知道为什么运行的时候就会报错呢,有没有人可以解答一下
十分感谢!!
排序的内循环j,len-i-i这里错了,应该是j<len-1-i