struct,自定义数据类型,可以存放不同数据类型
#include<iostream>
#include<string>
using namespace std;
struct Student {
string name;
int age;
}s3; // 第三种定义
int main () {
// 第一种
Student s1;
s1.name = "张三";
s1.age = 24;
cout << "s1的名字:" << s1.name << endl;
cout << "s1的年纪:" << s1.age << endl;
// 第二种
Student s2 = {"李四", 25};
cout << "s2的名字:" << s2.name << endl;
cout << "s2的年纪:" << s2.age << endl;
s3.name = "王五";
s3.age = 26;
// 第三种赋值
cout << "s3的名字:" << s3.name << endl;
cout << "s3的年纪:" << s3.age << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
struct Student {
string name;
int age;
};
struct Teacher {
string name;
int age;
struct Student std;
};
int main () {
// 结构体赋值
Teacher t1;
t1.name = "张三";
t1.age = 24;
t1.std.name = "李四";
t1.std.age = 10;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
struct Student {
string name;
int age;
};
int main () {
// 定义
Student s1[2] = {
{"张三", 25},
{"李四", 26},
};
// 赋值
s1[0].name = "张一一";
s1[0].age = 24;
}