09C++结构体

/*结构体属于用户自定义的数据类型, 允许用户存储不同的数据类型,

语法:struct 结构体名{结构体成员列表} ;*/

//struct 结构体名 变量名

#include <iostream>

#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;

return 0;

}


//struct 结构体名 变量名={成员1值,成员2值...}

#include <iostream>

#include <string>

using namespace std;

struct student

{ string name;int age;int score;};

int main()

{

struct student s2={"张三",18,100 };

cout<<"姓名:"<<s2.name<<" "<<"年龄:"<<s2.age<<" " <<"分数:"<<s2.score<<endl;

return 0;

}


//定义结构体时顺便创建变量

#include <iostream>

#include <string>

using namespace std;

struct student

{ string name;int age;int score;};

struct student s3;

int main()

{

s3.name="张三";

s3.age=18;

s3.score=100;

cout<<"姓名:"<<s3.name<<" "<<"年龄:"<<s3.age<<" " <<"分数:"<<s3.score<<endl;

return 0;

}


//结构体数组

#include <iostream>

using namespace std;

struct student {string name;int age;int score;};

int main()

{

//创建结构体数组,并给结构体数组的元素赋值

struct student s1[3]=

{

{"zhang",18,100},

{"li",19,99},

{"wang",20,98},

};

//遍历结构体数组

for(int i=0;i<3;i++)

{

cout<<"姓名:"<<s1[i].name <<"年龄: "<<s1[i].age<<"分数:"<<s1[i].score<<endl;

}

return 0;

}


//结构体指针

#include <iostream>

#include <string>

using namespace std;

struct student {string name;int age;int score;};

int main()

{

struct student s={"zhang",18,100};

//通过指针指向结构体变量

student *p=&s;

//通过指针访问结构体变量中的数据

cout<<"姓名: "<<p->name<<"年龄:"<<p->age<<"分数:"<<p->score<<endl;

return 0;

}


//结构体嵌套结构体

#include <iostream>

#include <string>

using namespace std;

struct student{string name;int age;};

struct teacher{string name;int age;struct student s;};

int main()

{

teacher t;

t.name="li";

t.age=43;

t.s.name="zhang";

t.s.age=18;

cout<<"姓名: "<<t.name<<"年龄:"<<t.age<<"学生姓名:"<<t.s.name<<"学生年龄:"<<t.s.age<<endl;

return 0;

}


//结构体数组

#include <iostream>

#include <string>

using namespace std;

//结构体数组------定义结构体数组

struct student{

string name;

int age;

int score;

};

//创建结构体数组

int main()

{

struct student stuarray[3]={

{

"zhang",18,100

},

{

"li",19,99

},

{

"wang",20,98

}

};

//结构体数组中的元素赋值

stuarray[2].name="zhao";

stuarray[2].age=21;

stuarray[2].score=97;

//遍历结构体数组

for(int i=0;i<3;i++){

cout<<"姓名: "<<stuarray[i].name

<<"年龄: "<<stuarray[i].age

<<"分数: "<<stuarray[i].score<<endl;

}

return 0;

}


//结构体指针

#include <iostream>

#include <string>

using namespace std;

struct student{

string name;

int age;

int score;

};

int main()

{

struct student s={"zhang",18,100

};

struct student*p=&s;//通过指针指向结构体变量

cout<<"姓名:"<<p->name<<" "<<"年龄:"<<p->age<<" "<<"分数:"<<p->score<<endl;

return 0;

}


//结构体嵌套结构体

#include <iostream>

#include <string>

using namespace std;

struct student{

string name;

int age;

int score;

};

struct teacher{

string name;

int id;

struct student stu;

};

int main()

{

teacher t;

t.id=10010;

t.name="ye";

t.stu.name="zou";

t.stu.age=18;

t.stu.score=100;

cout<<"老师姓名:"<<t.name<<" "<<"老师id:"<<t.id<<" "

<<"学生姓名:"<<t.stu.name<<" "<<"学生年龄:"<<t.stu.age<<" "

<<"学生分数:"<<t.stu.score <<endl;

return 0;

}


//结构体做函数参数

#include <iostream>

#include <string>

using namespace std;

struct student {

string name;

int age;

int score;

};

// 值传递

void printstudent(student s) {

cout << "子函数中姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;

}

// 地址传递

void printstudent1(student* p) {

cout << "子函数1中姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;

}

int main() {

student s;

s.name = "zhang";

s.age = 18;

s.score = 100;

// 调用值传递函数

printstudent(s);

// 调用地址传递函数

printstudent1(&s);

return 0;

}

#include <iostream>

#include <string>

using namespace std;

struct student {

string name;

int age;

int score;

};

// 值传递

void printstudent(student s) {

s.age=100;//值传递修饰形参,实参不会改变

cout << "子函数中姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;

}

// 地址传递

void printstudent1(student* p) {

cout << "子函数1中姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;

}

int main() {

student s;

s.name = "zhang";

s.age = 18;

s.score = 100;

// 调用值传递函数

printstudent(s);

cout<<"年龄:"<<s.age<<endl;

// 调用地址传递函数

printstudent1(&s);

return 0;

}

#include <iostream>

#include <string>

using namespace std;

struct student {

string name;

int age;

int score;

};

// 值传递

void printstudent(student s) {

cout << "子函数中姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;

}

// 地址传递

void printstudent1(student* p) {

p->age=28;

cout << "子函数1中姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;

}

int main() {

student s;

s.name = "zhang";

s.age = 18;

s.score = 100;

// 调用值传递函数

printstudent(s);

// 调用地址传递函数

printstudent1(&s);

cout<<"年龄:"<<s.age<<endl;

return 0;

}

//结构体const的使用

#include <iostream>

#include <string>

using namespace std;

struct student {

string name;

int age;

int score;

};

void printstudent(const student *s){//加入const之后,一旦有修改的操作就会报错,防止误操作

cout<<"姓名:"<<s->name<<" "<<"年龄:"<<s->age<<"分数:"<<s->score<<endl;

}

int main() {

struct student s={"zhang",18,100

};

printstudent(&s);

return 0;

}

相关推荐
奔跑吧邓邓子19 分钟前
【Python爬虫(34)】Python多进程编程:开启高效并行世界的钥匙
开发语言·爬虫·python·多进程
刃神太酷啦1 小时前
堆和priority_queue
数据结构·c++·蓝桥杯c++组
Heris991 小时前
2.22 c++练习【operator运算符重载、封装消息队列、封装信号灯集】
开发语言·c++
----云烟----1 小时前
C/C++ 中 volatile 关键字详解
c语言·开发语言·c++
yuanpan1 小时前
23种设计模式之《组合模式(Composite)》在c#中的应用及理解
开发语言·设计模式·c#·组合模式
BanLul1 小时前
进程与线程 (三)——线程间通信
c语言·开发语言·算法
十八朵郁金香1 小时前
【JavaScript】深入理解模块化
开发语言·javascript·ecmascript
Hello.Reader2 小时前
深入理解 Rust 的 `Rc<T>`:实现多所有权的智能指针
开发语言·后端·rust
程序员阿鹏2 小时前
jdbc批量插入数据到MySQL
java·开发语言·数据库·mysql·intellij-idea
yoona10202 小时前
Rust编程语言入门教程(八)所有权 Stack vs Heap
开发语言·后端·rust·区块链·学习方法