结构体
基本概念:结构体属于用户自定义的数据类型,允许用户储存不同的数据类型。
1、结构体的定义和使用:
#include <string>
//C++中使用字符串需要添加string头文件
#include "iostream"
using namespace std;
//1、创建学生数据类型,包括姓名年龄分数
//struct 类型名称 { 成员列表 }
struct Student
{
string name;
int age;
int score;
}wangwu;
//创建结构体的时候顺便添加成员(较少用)
int main()
{
//2、创建具体学生
struct Student zhangsan;
//创建成员时struct可以省略
//给zhangsan赋值,通过zhangsan.xx访问结构体变量中的属性
zhangsan.name = "张三" ;
zhangsan.age = 19;
zhangsan.score = 90;
struct Student lisi = {
"李四",18,92 };
//创建成员时直接赋值
return 0 ;
}
2、结构体数组
#include <string>
//C++中使用字符串需要添加string头文件
#include "iostream"
using namespace std;
struct Student
{
string name;
int age;
int score;
};
int main(