结构体属于用户自定义的数据类型,允许用户存储不同的数据类型。
语法: struct 结构体名 { 结构体成员列表 };
二、C语言、C++结构体、C++类的区别C++里根本就没有结构,仅仅为了兼容c才有结构这种东西,实际上除了名字以外,C++的结构就是类。
C的结构体和C++结构体的区别
- C的结构体内不允许有函数存在,C++允许有内部成员函数,且允许该函数是虚函数。所以C的结构体是没有构造函数、析构函数、和this指针的。
- C的结构体对内部成员变量的访问权限只能是public,而C++允许public、protected、private三种。
- C不是面向对象的语言,没有继承等概念。C语言的结构体是不可以继承的,C++的结构体是可以从其他的结构体或者类继承过来的。C++,结构体除了可以包含变量外,还可以包含函数,能继承,能实现多态
C++的struct、C++的class区别
- 如果从空间分配上来看:struct变量放在栈上,而class变量放在堆上,因此struct变量会自动释放,而class变量需要手动释放。
- 在C++中,struct和class没啥两样,只是struct默认是public,class默认是private。
- struct内部成员变量、成员函数的默认访问级别是public类型的;
- class内部成员变量、成员函数的默认访问级别是private类型的;
1.利用结构体自带的默认构造函数
2.利用带参数的构造函数
3.利用自定义的初始化函数
#includeusing namespace std; struct node { int data; string str; char x; } Node[2]; int main() { Node[0] = {1, "123a", 'a'}; //利用结构体自带的默认构造函数 Node[1] = {2, "c++", 'd'}; //利用结构体自带的默认构造函数 for (int i = 0; i < 2; i++) { cout << Node[i].data << " " << Node[i].str << " " << Node[i].x << endl; } return 0; }
打印结果:
1 123a a 2 c++ d Process finished with exit code 02、利用带参数的构造函数
链表节点:
struct ListNode {
int val;
ListNode *next; // 节点指针
ListNode() :val(), next(){}//无参构造,初始化时调用
ListNode(int x) : val(x), next(NULL) {}//有参构造
};
#includeusing namespace std; struct node { int data; string str; char x; node(int a, string b, char c) : data(a), str(b), x(c) {} //有参构造【注意构造函数最后这里没有分号!】 }; int main() { node Node = node(4, "Java", '5');//有参数结构体构造函数 cout << Node.data << " " << Node.str << " " << Node.x << endl; return 0; }
打印结果:
4 Java 5 Process finished with exit code 0
在建立结构体数组时,如果只写了带参数的构造函数将会出现数组无法初始化的错误!!!
#includeusing namespace std; struct node { int data; string str; char x; node() : x(), str(), data() {} // 在建立结构体数组时, 结构体中一定要有显式的无参构造函数!!! node(int a, string b, char c) : data(a), str(b), x(c) {} //有参构造【注意构造函数最后这里没有分号!】 } Node[3]; int main() { Node[0] = {1, "123a", 'a'}; //无参默认结构体构造体函数 Node[1] = {2, "c++", 'd'}; //无参默认结构体构造体函数 Node[2] = node(4, "Java", '5');//有参数结构体构造函数 for (int i = 0; i < 3; i++) { cout << Node[i].data << " " << Node[i].str << " " << Node[i].x << endl; } return 0; }
打印结果:
1 123a a 2 c++ d 4 Java 5 Process finished with exit code 03、利用自定义的初始化函数
#includeusing namespace std; struct node { int data; string str; char x; //自己写的初始化函数 void init(int a, string b, char c) { this->data = a; this->str = b; this->x = c; } } Node[3]; int main() { Node[0].init(1, "123a", 'a'); //自定义初始化函数的调用 Node[1].init(2, "c++", 'd'); //自定义初始化函数的调用 Node[2].init(3, "123456", '9'); //自定义初始化函数的调用 for (int i = 0; i < 3; i++) { cout << Node[i].data << " " << Node[i].str << " " << Node[i].x << endl; } return 0; }
打印结果:
1 123a a 2 c++ d 3 123456 9 Process finished with exit code 0四、通过结构体创建变量的方式(3种)
结构体使用规则:
- 定义结构体时关键字是struct,不可省略
- 创建结构体变量时,关键字struct可以省略
- 结构体变量使用操作符"."访问成员
通过结构体创建变量的方式01:struct 结构体名 变量名
#includeusing namespace std; //全局变量声明区 //函数声明区 //结构体定义 struct student { //成员列表 string name; //姓名 int age; //年龄 int score; //分数 }; int main() { //结构体变量创建方式1 struct student stu1; //struct 关键字可以省略不写 stu1.name = "张三"; stu1.age = 18; stu1.score = 100; cout << "姓名:" << stu1.name << " 年龄: " << stu1.age << " 分数: " << stu1.score << endl; return 0; }
打印结果:
姓名:张三 年龄: 18 分数: 100 Process finished with exit code 02、通过结构体创建变量的方式02
通过结构体创建变量的方式02:struct 结构体名 变量名 = {成员1值,成员2值}
#includeusing namespace std; //全局变量声明区 //函数声明区 //结构体定义 struct student { //成员列表 string name; //姓名 int age; //年龄 int score; //分数 }; int main() { //结构体变量创建方式2 struct student stu2 = {"李四", 19, 60}; cout << "姓名:" << stu2.name << " 年龄: " << stu2.age << " 分数: " << stu2.score << endl; return 0; }
打印结果:
姓名:李四 年龄: 19 分数: 60 Process finished with exit code 03、通过结构体创建变量的方式03
通过结构体创建变量的方式02:定义结构体时顺便创建变量
#includeusing namespace std; //全局变量声明区 //函数声明区 //结构体定义 struct student { //成员列表 string name; //姓名 int age; //年龄 int score; //分数 } stu3; //结构体变量创建方式3 int main() { stu3.name = "王五"; stu3.age = 18; stu3.score = 80; cout << "姓名:" << stu3.name << " 年龄: " << stu3.age << " 分数:" << stu3.score << endl; return 0; }
打印结果:
姓名:王五 年龄: 18 分数:80 Process finished with exit code 0五、结构体数组
作用:将自定义的结构体放入数组中方便维护。
语法:struct 结构体名 数组名[元素个数] = { {}, {}, .....{} }
#includeusing namespace std; //全局变量声明区 //函数声明区 //结构体声明 struct student { //成员列表 string name; //姓名 int age; //年龄 int score; //分数 }; int main() { //结构体数组 struct student arr[3] = { {"张三", 18, 80}, {"李四", 19, 60}, {"王五", 18, 70} }; for (int i = 0; i < 3; i++) { cout << "姓名:" << arr[i].name << " 年龄:" << arr[i].age << " 分数;" << arr[i].score << endl; } return 0; }
打印结果:
姓名:张三 年龄:18 分数;80 姓名:李四 年龄:19 分数;60 姓名:王五 年龄:18 分数;70 Process finished with exit code 0六、结构体指针
作用:通过指针访问结构体中的成员。利用操作符 ->可以通过结构体指针访问结构体属性。
结构体指针可以通过 -> 操作符访问结构体中的成员。
#includeusing namespace std; //全局变量声明区 //函数声明区 //结构体定义 struct student { //成员列表 string name; //姓名 int age; //年龄 int score; //分数 }; int main() { struct student stu = {"张三", 18, 100}; struct student *p = &stu; p->score = 80; //指针通过->操作符可以访问成员 cout << "姓名: " << p->name << " 年龄: " << p->age << " 分数: " << p->score << endl; system("pause"); return 0; }
打印结果:
姓名: 张三 年龄: 18 分数: 80 Process finished with exit code 0七、“结构体”嵌套“结构体”
在结构体中可以定义另一个结构体作为成员,用来解决实际问题。
作用:结构体中的成员可以是另一个结构体。
例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体。
#include//** 例如:** 每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体 using namespace std; //全局变量声明区 //函数声明区 //学生结构体 struct student { //成员列表 string name; //姓名 int age; //年龄 int score; //分数 }; //教师结构体定义 struct teacher { //成员列表 int id; //职工编号 string name; //教师姓名 int age; //教师年龄 struct student stu; //子结构体 学生 }; int main() { struct teacher t1; t1.id = 10000; t1.name = "老王"; t1.age = 40; t1.stu.name = "张三"; t1.stu.age = 18; t1.stu.score = 100; cout << "教师 职工编号:" << t1.id << " 姓名: " << t1.name << " 年龄: " << t1.age << endl; cout << "辅导学员 姓名:" << t1.stu.name << " 年龄:" << t1.stu.age << " 考试分数: " << t1.stu.score << endl; return 0; }
打印结果:
教师 职工编号:10000 姓名: 老王 年龄: 40 辅导学员 姓名:张三 年龄:18 考试分数: 100 Process finished with exit code 0八、结构体做函数参数
作用:将结构体作为参数向函数中传递。
传递的方式有两种:
- 值传递
- 地址传递
#includeusing namespace std; //全局变量声明区 //学生结构体定义 struct student { string name; int age; int score; }; //函数声明 void printfStudent(student stu); void printfStudent2(student *stu); int main() { student stu = {"张三", 18, 100}; cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl; cout << endl; //值传递 printfStudent(stu); cout << "值传递---->主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl; cout << endl; //地址传递 printfStudent2(&stu); cout << "地址传递---->主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl; return 0; } //值传递 void printfStudent(student stu) { stu.age = 28; cout << "值传递---->子函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl; } //地址传递 void printfStudent2(student *stu) { stu->age = 28; cout << "地址传递---->子函数中 姓名:" << stu->name << " 年龄: " << stu->age << " 分数:" << stu->score << endl; }
打印结果:
主函数中 姓名:张三 年龄: 18 分数:100 值传递---->子函数中 姓名:张三 年龄: 28 分数:100 值传递---->主函数中 姓名:张三 年龄: 18 分数:100 地址传递---->子函数中 姓名:张三 年龄: 28 分数:100 地址传递---->主函数中 姓名:张三 年龄: 28 分数:100 Process finished with exit code 0
参考资料:
C++结构体详解
结构体构造函数
C++ 结构体构造函数
C++结构体完全攻略(超详细)
C++学习 — 结构体
C++ - 结构体构造函数使用总结



