|
#include <iostream>
#include <iomanip>
#include<string>
using namespace std;
struct Student
{
//成员列表
//姓名
string name;
//年龄
int age;
//分数
int score;
};
int main()
{
struct Student s1;
s1.name = "张三";
s1.age = 18;
s1.score = 100;
cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;
system("pause");
return 0;
}
2:结构体数组------类似数组
struct Student
{
//成员列表
//姓名
string name;
//年龄
int age;
//分数
int score;
};
int main()
{
//创建结构体数组
struct Student stuArray[3] =
{
{"张三",18,100},
{"李四",28,99},
{"王五",38,66}
};
//3.给结构体数组中的元素赋值
stuArray[2].name = "赵六";
stuArray[2].age = 80;
stuArray[2].score = 60;
for (int i = 0; i < 3; i++)
{
cout << "姓名:"<<stuArray[i].name<<" 年龄:"<<stuArray[i].age<<" 分数:"<<stuArray[i].score<<endl;
}
3:结构体指针
//定义学生结构体
struct Student
{
string name;//姓名
int age;//年龄
int score;//分数
};
int main()
{
//创建学生结构体变量
Student s = { "张三",18,100 };
//通过指针指向结构体变量
Student* p = &s;
//通过指针访问结构体变量中的数据
//通过结构体指针 访问结构体中的属性,需要利用->
cout << "姓名:"<<p->name<<" 年龄:"<<p->age<<" 分数:"<<p->score<< endl;
system("pause");
return 0;
}
|
上一篇: C++中iomanip用法下一篇: SQL 中的modify使用问题
|