结构体
1.定义和使用
用户自定义的数据结构
struct Student {string name; int age; float score;};
具体创建三法:
1.struct Student s1; s1.name = zhangsan ; s1.age = 8; s1.score = 99;
2.struct Student s2 = {liming,8,99}
3.定义结构体顺便创建struct Student {string name; int age; float score;}s3;
struct在创建时可省略,定义时不可省略。
2.结构体数组
#include<iostream>
using namespace std;
#include<string>
struct student {
string name;
int age;
float score;
};
int main() {
student arr[3] = {
{"liming",17,60},{"zhanghua",16,87},{"chenshu",18,98}
};
cout << arr[2].name << endl;
}
3.结构体指针
->
using namespace std;
#include<string>
struct student {
string name;
int age;
float score;
};
int main() {
student s = { "liming",17,87 };
(struct) student* p = &s;
cout << p->age << endl;
}
4.嵌套结构体
using namespace std;
#include<string>
struct student {
string name;
int age;
float score;
};
struct teacher {
int id;
string name;
int age;
struct student stu;
};
int main() {
teacher t;
t.id = 10086;
t.name = "Huang wuchang";
t.stu.name = "liming";
s.stu.age = 20;
t.stu.score = 60;
}
5.结构体做函数参数
值传递和地址传递
#include<iostream>
using namespace std;
#include<string>
struct student {
string name;
int age;
float score;
};
void printStudent1(struct student s) {
s.age = 100;
cout << " 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;
}
void printStudent2(struct student* p) {
p->age = 200;
cout<< "地址访问" << endl;
cout << " 姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
}
int main() {
student s;
s.name = "liming";
s.age = 18;
s.score = 60;
printStudent1(s);
//printStudent2(&s);//地址访问
cout << s.age;
}
6.const使用场景
using namespace std;
#include<string>
struct student {
string name;
int age;
float score;
};
void printStudent(const student *s) {
s->age = 150;//const不能再做操作
cout << " 姓名:" << s->name << " 年龄:" << s->age << " 分数:" << s->score << endl;
}
int main() {
student s = { "liming",17,60 };
printStudent(&s);
}//会报错
7.实例
1:
#include<iostream>
using namespace std;
#include<string>
struct student {
string name;
float score;
};
struct teacher {
string name;
struct student sArray[5];
};
void allocateSpace(struct teacher tArray[],int len) {
string nameSeed = "ABCDE";
for (int i = 0; i<len; i++) {
tArray[i].name = "Teacher";
tArray[i].name += nameSeed[i];
for (int j = 0; j < 5; j++) {
tArray[i].sArray[j].name = "student_";
tArray[i].sArray[j].name += nameSeed[j];
int random = rand() % 60 + 50;//0~59 + 40
tArray[i].sArray[j].score = random;
}
}
}
void printInfo(struct teacher tArray[], int len) {
for (int i = 0; i < len; i++) {
cout << "老师姓名:" << tArray[i].name<<endl;
for (int j = 0; j < 5; j++) {
cout << "\t学生姓名:" << tArray[i].sArray[j].name << " 学生分数:" << tArray[i].sArray[j].score << endl;
}
}
}
int main() {
//三名老师,每个老师带三个学生
//随机数种子
srand((unsigned int)time(NULL));
teacher tArray[3];
int l = sizeof(tArray) / sizeof(tArray[0]);
allocateSpace(tArray, l);
printInfo(tArray, l);
}
2:按年龄升序
#include<iostream>
using namespace std;
#include<string>
struct hero {
string name;
int age;
string sex;
};
void bubbleSort(hero heroArray[], int l) {
for(int i = 0; i < l-1; i++) {
for (int j = 0; j < l - i - 1; j++) {
if (heroArray[j].age > heroArray[j + 1].age) {
struct hero temp = heroArray[j];
heroArray[j] = heroArray[j + 1];
heroArray[j + 1] = temp;
}
}
}
}
void printHero(hero heroArray[], int l) {
for (int i = 0; i < l; i++) {
cout << "名字:" << heroArray[i].name << " 年龄:" << heroArray[i].age << " 性别:" << heroArray[i].sex << endl;
};
}
int main() {
hero heroArray[5] = {
{"貂蝉",19,"女"},
{"赵云",21,"男"},
{"张飞",20,"男"},
{"关羽",22,"男"},
{"刘备",23,"男"}
};
int l = sizeof(heroArray) / sizeof(heroArray[0]);
printHero(heroArray, l);
bubbleSort(heroArray, l);
cout << endl;
printHero(heroArray, l);
}