结构体
自定义的数据类型,可存储不同的数据类型。
1、结构体定义和使用
#include <iostream>
#include "swap.h"
using namespace std;
#define week 7
#include <string>
struct student
{
string name;
int age;
int score;
};
int main()
{
struct student s1;
s1.name = "张同学";
s1.age = 34;
s1.score = 80;
cout << "姓名:" << s1.name << "" << s1.age << "" << s1.score << endl;
struct student s2 = {"二涛",32,85};
cout << "姓名:" << s2.name << "" << s2.age << "" << s2.score << endl;
system("pause");
return 0;
}
2、结构体数组
#include <iostream>
#include "swap.h"
using namespace std;
#define week 7
#include <string>
struct student
{
string name;
int age;
int score;
};
int main()
{
struct student stuary[3] =
{
{"小明",20,80},
{"小亮",20,85},
{"小华",20,89},
};
for (int i = 0; i < 3; i++)
{
cout << "name = " << stuary[i].name << endl;
cout << "age =" << stuary[i].age << endl;
cout << "score =" << stuary[i].score << endl;
}
system("pause");
return 0;
}
3、结构体指针
#include <iostream>
#include "swap.h"
using namespace std;
#define week 7
#include <string>
struct student
{
string name;
int age;
int score;
};
int main()
{
struct student stuary[3] =
{
{"小明",20,80},
{"小亮",20,85},
{"小华",20,89},
};
struct student* p = &stuary[0];
cout << "name=" << p->name << "age=" << p->age << "score=" << p->score << endl;
system("pause");
return 0;
}
4、结构体做函数参数
#include <iostream>
#include "swap.h"
using namespace std;
#define week 7
#include <string>
struct student
{
string name;
int age;
int score;
};
void print_sty(struct student s)
{
cout << "值传递 s.name=" << s.name << "s.age=" << s.age << "s.score=" << s.score << endl;
}
void print_sty_2(struct student *p)
{
cout << "地址传递s.name=" << p->name << "s.age=" << p->age << "s.score=" << p->score << endl;
}
int main()
{
struct student stuary[3] =
{
{"小明",20,80},
{"小亮",20,85},
{"小华",20,89},
};
print_sty(stuary[0]);
struct student *p = &stuary[0];
print_sty_2(p);
system("pause");
return 0;
}
5、结构体嵌套结构体
#include <iostream>
#include "swap.h"
using namespace std;
#define week 7
#include <string>
struct student
{
string name;
int age;
int score;
};
struct teacher
{
int id;
string name;
int age;
struct student student1;
};
int main()
{
struct student stuary[3] =
{
{"小明",20,80},
{"小亮",20,85},
{"小华",20,89},
};
struct student* p = &stuary[0];
teacher t = { 100,"老徐",50,stuary[1] };
cout << "id=" << t.id << "name=" << t.name << "age=" << t.age
<< "t.student1.name=" << t.student1.name << "t.student1.age=" << t.student1.age << "t.student1.score=" << t.student1.score <<endl;
system("pause");
return 0;
}
6、结构体中const使用场景
#include <iostream>
#include "swap.h"
using namespace std;
#define week 7
#include <string>
struct student
{
string name;
int age;
int score;
};
void printstu(const student *p)
{
cout << "name=" << p->name << "age=" << p->age << "score=" << p->score << endl;
}
int main()
{
struct student stuary[3] =
{
{"小明",20,80},
{"小亮",20,85},
{"小华",20,89},
};
printstu(&stuary[0]);
system("pause");
return 0;
}
结构体案例
#include <iostream>
#include "swap.h"
using namespace std;
#define week 7
#include <string>
struct student
{
string name;
int score;
};
struct teacher
{
string name;
struct student stu[5];
};
void allinfo(struct teacher t[], int len)
{
string nameseed = "ABCDEF";
for (int i = 0; i < len; i++)
{
t[i].name = "teacher";
t[i].name += nameseed[i];
for (int j = 0; j < 5; j++)
{
t[i].stu[j].name = "stdent_";
t[i].stu[j].name += nameseed[i];
int random = rand() % 60+40;
t[i].stu[j].score = random;
}
}
}
void printinfo(struct teacher t[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "name=" << t[i].name;
for (int j = 0; j < 5; j++)
{
cout << "学生name=" << t[i].stu[j].name;
cout << "学生score=" << t[i].stu[j].score << endl;
}
}
}
int main()
{
struct teacher teacherary[3];
allinfo(teacherary, 3);
printinfo(teacherary, 3);
system("pause");
return 0;
}
|