当前位置: 首页 > news >正文

贵州建网站的公司itme收录优美图片官网

贵州建网站的公司,itme收录优美图片官网,营销型网站建设搭建方法,网站怎么做国际化结构体 //一、结构体的概念、定义和使用 // 概念&#xff1a;结构体属于用户自定义的数据类型&#xff0c;允许用户存储不同的数据类型 #include<iostream> using namespace std; #include<string> //1.创建学生数据类型&#xff1a;学生包括&#xff08;姓名&am…

结构体

//一、结构体的概念、定义和使用


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

#include<iostream>
using namespace std;
#include<string>
//1.创建学生数据类型:学生包括(姓名,年龄,分数)
//自定义数据类型,一些类型集合组成的一个类型
//语法 strcut 类型名称{成员列表};  注意后面有;
struct Student
{//成员列表//姓名string name;//年龄int age;//分数int score;
}s3;//2.通过学生类型创建具体学生int main()
{
//2.1 struct Student s1//strcut关键字可以省略//struct Student s1;Student s1;//给s1属性赋值,通过.访问结构体变量中的属性s1.name = "张三";s1.age = 18;s1.score = 100;cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;
//2.2 struct Student s2={...}struct Student s2 = { "李四",19,80 };cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl;//2.3 在定义结构体时顺便创建结构体变量(如果没有s3,则上面数据{}后要加;s3.name = "王五";s3.age = 20;s3.score = 60;cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl;//一般用第1或2种return 0;
}

//二、结构体数组


//作用:将自定义的结构体放入到数组中方便维护
//语法:struct 结构体名 数组名{元素个数}={ {},{},...{} }

#include<iostream>
using namespace std;//结构体数组
//1.定义结构体
struct Student
{string name;int age;int score;
};
int main()
{//2.创建结构体数组struct Student stuArray[3] ={{"张三",18,100},{"李四",28,99},{"王五",38,66}};//3.给结构体数组中的元素赋值//一开始不知道人家名字,后面改stuArray[2].name = "赵六";stuArray[2].age = 80;stuArray[2].score = 80;//4.便利结构体数组for (int i = 0; i < 3; i++){cout    << " 姓名: " << stuArray[i].name << " 年龄: " << stuArray[i].age << " 分数: " << stuArray[i].score << endl;}return 0;
}

//三、结构体指针


//作用:通过指针访问结构体中的成员 利用操作符->

#include<iostream>
using namespace std;struct student
{string name;int age;int scroe;
};
int main()
{//1.创建学生结构体变量struct student s = { "张三",18,100 };//struct可省略//2.通过指针指向结构体变量struct student* p = &s;//struct可省略//3.通过指针访问结构体变量中的数据//通过结构体指针 访问结构体中的属性,需要利用'->'cout << "姓名: " << p->name << " 年龄: " << p->age << " 分数: " << p->scroe << endl;return 0;
}

//四、结构体嵌套结构体


//作用:结构体中的成员可以是另一个结构体

#include<iostream>
using namespace std;
#include<string>struct student
{string name;int age;int score;
};
struct teacher
{int id;string name;int age;struct student s;
};int main()
{teacher t;t.id = 18888;t.name = "老王";t.age = 50;t.s.name = "小王";t.s.age = 20;t.s.score = 60;cout << "老师姓名: " << t.name << " 老师编号: " << t.id << " 老师年龄: " << t.age<< " 老师辅导学生姓名:  " << t.s.name << " 学生年龄: " << t.s.age << "  学生分数: " << t.s.score << endl;return 0;
}

//五、结构体做函数参数


//作用:将结构体作为参数向函数中传递
//传递方式有两种:值传递和地址传递

#include<iostream>
using namespace std;
#include<string>struct student
{string name;int age;int score;
};
//打印学生信息函数
//1.值传递
void printStudent1(struct student s)
{s.age = 100;cout << " 子函数1中 姓名:" << s.name << " 年龄:" << s.age << " 分数: " << s.score << endl;
}
//2.地址传递
void printStudent2(struct student * p)
{p->age = 200;cout << "子函数2中 姓名:" << p->name << " 年龄: " << p->age << " 分数: " << p->score << endl;
}
int main()
{//结构题做函数参数//将学生传入到一个参数中,打印学生身上的所有信息//创建一个结构体变量struct student s;s.name = "张三";s.age = 18;s.score = 85;printStudent1(s);printStudent2(&s);cout << "main函数中打印 姓名:" << s.name << " 年龄: " << s.age << " 分数:" << s.score<<endl;return 0;
}

//六、结构体中const使用场景


//作用:用const来防止误操作

#include<iostream>
using namespace std;
#include<string>//const的使用场景struct student
{string name;int age;int score;
};//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来
void printStudent(const student *s)//指针目的是节省空间,const是防止修改了形参把实参变了,即防止误操作
{s->age = 150;//(误操作了)//但是利用了const age还是为15岁  只能读不能写cout << "姓名: " << s->name << " 年龄: " << s->age << " 得分: " << s->score << endl;
}int main()
{//创建结构体变量struct student s = { "张三",15,70 };//通过函数打印结构变量信息printStudent(&s);cout << s.age;return 0;
}

http://www.hkea.cn/news/437882/

相关文章:

  • java web开发网站开发cpa推广接单平台
  • 广西南宁网络营销网站网站权重优化
  • 黄山网站设计公司营销网站建设多少钱
  • 网站建设招标评分表湖南关键词优化推荐
  • 淘宝上成都网站建设如何制作视频网站
  • 最吃香的男生十大手艺5g网络优化
  • 河源哪里做网站网络项目怎么推广
  • 网站闭关保护怎么做广州百度seo 网站推广
  • 可以在线做动图的网站近期重大新闻事件
  • 伊犁州建设局网站怎么做微信小程序
  • 做网站需要买主机那新媒体营销方式有几种
  • 网络推广seo公司seo排名的方法
  • 南山做网站多少钱百度资讯
  • 西安哪里有做网站的小学生收集的新闻10条
  • 做游戏网站有几个要素seo网站关键词优化报价
  • 蓬业东莞网站建设技术支持东莞做网站公司首选
  • 网站版式设计获客渠道有哪些
  • 今日军事新闻简短扬州seo优化
  • 国外好看的教育类网站模板下载东莞做网站最好的是哪家
  • 微擎与wordpress快速优化seo软件推广方法
  • 英文网站设计哪家好免费网站搭建
  • 网站建设公司 销量深圳谷歌seo公司
  • 新蔡哪有做网站建设的全球疫情今天最新消息
  • 怎么做平台网站百度seo报价方法
  • 帮人做网站 怎么收费怎么用网络推广
  • 网站排名优化建设百度广告投放技巧
  • 文件服务器网站搭建教程好的竞价托管公司
  • 黑龙江省城乡和住房建设厅网站首页百度链接地址
  • 网站模板修改工具专业seo关键词优化
  • 口碑好的句容网站建设yahoo搜索