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

滁州市建设工程质量监督站网站博客程序seo

滁州市建设工程质量监督站网站,博客程序seo,网站开发后端用什么技术,抖音代运营协议书范本文章目录参考描述结构体使用(基本)声明初始化先创建后初始化C 11 列表初始化使用(进阶)结构数组声明(拓展)声明及创建声明及初始化匿名结构体细节外部声明与内部声明成员赋值共用体存储空间匿名共用体同化尾…

文章目录

  • 参考
  • 描述
  • 结构体
  • 使用(基本)
      • 声明
      • 初始化
          • 先创建后初始化
          • C++ 11 列表初始化
  • 使用(进阶)
      • 结构数组
      • 声明(拓展)
          • 声明及创建
          • 声明及初始化
          • 匿名结构体
  • 细节
      • 外部声明与内部声明
      • 成员赋值
  • 共用体
      • 存储空间
      • 匿名共用体
          • 同化
  • 尾声

参考

项目描述
搜索引擎Bing
C++ Primer Plus(第六版)史蒂芬·普拉达

描述

项目描述
g++8.1.0
操作系统Windows 10 专业版

结构体

在 C++ 中,能够同时存储多个元素的复合类型除了数组外还有结构体。两者的区别在于:

  1. 一个数组中的每一个元素都必须属于同一种数据类型。你可以创建一个整型数组,但其中不能出现除整型数据类型外的其他数据类型,比如单精度浮点型(float)及字符型(char)。

  2. 一个结构体中可以同时存在多个元素,且这些元素的数据类型没有限制。

使用(基本)

声明

声明一个结构体时你需要使用到 struct 关键字。

举个栗子

// 导入 iostream 以使 C++ 程序具有基本的输入输出功能
#include <iostream>
// 导入 string 以使用 String 类创建字符串
// 使用 String 类创建字符串将获得更多的支持
#include <string>
// 使用标准名称空间 std
using namespace std;int main() {// 声明一个结构体struct Mark{// 在结构体中声明一个用于存放字符串的变量string name;// 在结构体中声明一个用于存放整型数据的变量int age;};  // 注意这个分号// 使用 system() 函数以使得窗口暂停system("pause");
}

注:

  1. 使用 struct 关键字创建了一个结构体后,你需要使用一个名称去对其进行标记。在本示例中我们将声明的结构体标记为 Mark

  2. 在结构体中声明的变量可以使用所有的 C++ 所支持的类型。

  3. 在声明结构体后,请不要忘记末尾的分号。将该分号省略,C++ 将会抛出错误。

初始化

先创建后初始化

举个栗子

#include <iostream>
#include <string>
using namespace std;int main() {// 声明结构体struct Info{string name;int age;};// 创建结构体Info info;// 此时 info 中的相关数据尚未被初识化,// 故从 info 中取出的数据不能确定。cout << info.name << endl;cout << info.age << endl;// 初始化结构体info.name = "RedHeart";info.age = 18;cout << "-------------***---------------" << endl;cout << info.name << endl;cout << info.age << endl;system("pause");
}

执行效果


8
-------------***---------------
RedHeart
18
请按任意键继续. . .
C++ 11 列表初始化
#include <iostream>
#include <string>
using namespace std;int main() {struct Info{string name;int age;};// 通过初始化列表对结构体进行初始化操作Info info = {"RedHeart",18};// 对结构体中的数据进行访问cout << info.name << endl;cout << info.age << endl;system("pause");
}

执行效果

RedHeart
18
请按任意键继续. . .

注:

  1. 使用列表初始化方式对结构体进行初始化时不能发生同类数据类型之间的缩窄转换(范围,精度),即不能将(仅列出部分):

    • 浮点型字面量(也可以称之为常量,即直接在程序中写出的数据)提供给整型变量。
    • 整型常量提供给字符型变量。
    • 双精度浮点型字面量提供给单精度浮点型常量。
  2. 使用列表初始化对结构体进行初始化但未对结构体中的变量指定值,则该变量将被初始化为 0 。例如:

#include <iostream>
#include <string>
using namespace std;int main() {struct Info{string name;int age;};// 使用列表初始化为结构体进行初始化操作,但未// 为 age 变量指定值,这将导致该变量被初始化为零。Info info = {"RedHeart"};Info test = {};cout << info.name << endl;cout << info.age << endl;system("pause");
}

执行结果

RedHeart
0
请按任意键继续. . .
  1. 使用列表初识化对结构体进行初识化操作时,关键字 struct= 均可以省略。

    • 完整形式
    // 声明一个结构体
    struct Info{};// 对结构体进行初始化
    struct Info info = {};
    
    • 简写形式
    // 声明一个结构体
    struct Info{};// 对结构体进行初始化
    // struct 关键字仅能在初始化过程中被省略    
    Info info {};
    

使用(进阶)

结构数组

你可以创建一个元素为结构体的数组,方法和创建基本类型数组完全相同。

举个栗子

#include <iostream>
#include <string>
using namespace std;int main() {struct Arr{string name;int age;};// 创建长度为 2 的结构数组Arr arr[2] {{"RedHeart", 18},{"TwoMoons"}};// 对结构数组中的元素进行访问cout << arr[0].name << '\t' << arr[0].age << endl;cout << arr[1].name << '\t' << arr[1].age << endl;system("pause");
}

执行结果

RedHeart        18
TwoMoons        0
请按任意键继续. . .

声明(拓展)

在前面,我们都是先对结构体进行声明后对其实现创建及初始化的。但其实,结构体的声明与结构体的创建及结构体的初始化是可以同时进行的。

声明及创建
#include <iostream>
#include <string>
using namespace std;int main() {// 在对 Info 结构体进行声明时同时创建 info 及 test 结构体struct Info{string name;int age;}info, test;// 对未初始化的结构体进行访问cout << info.name << '\t' << info.age << endl;cout << test.name << '\t' << test.age << endl;system("pause");
}

执行结果

        84200288
请按任意键继续. . .
声明及初始化
#include <iostream>
#include <string>
using namespace std;int main() {// 在对 Info 结构体进行声明时同时初始化 info 及 test 结构体struct Info{string name;int age;}info{"RedHeart",18}, test{"TwoMoons"};// 对未初始化的结构体进行访问cout << info.name << '\t' << info.age << endl;cout << test.name << '\t' << test.age << endl;system("pause");
}

执行结果

RedHeart        18
TwoMoons        0
请按任意键继续. . .
匿名结构体

你可以不用为定义的结构体类型赋予标记,但这将导致你在使用该结构类型创建结构体后无法再次使用该结构类型创建结构体。

举个栗子

#include <iostream>
#include <string>
using namespace std;int main() {// 声明并初始化结构体struct{string name;int age;}info{"RedHeart",18},test{"TwoMoons"};// 访问结构体中的变量cout << info.name << '\t' << info.age << endl;cout << test.name << '\t' << test.age << endl;system("pause");
}

执行结果

RedHeart        18
TwoMoons        0
请按任意键继续. . .

细节

外部声明与内部声明

在声明结构体时,你可以选择使用内部声明(即在函数内部对结构体进行声明)的方式对结构体进行声明,也可以选择使用外部声明(即在函数外部对结构体进行声明)的方式对结构体进行声明。

外部声明与内部声明的区别有:

  1. 通过外部声明的方式声明的结构体可以被多个函数所共享。在使用外部声明的方式声明结构体时,推荐将声明结构体的语句放置于所有函数之前,这样可以使得 C++ 中的所有函数都可以直接使用该结构体。

举个栗子

#include <iostream>
#include <string>
using namespace std;struct GlobalStruct{string name;int age;
};void func() {// 使用这部分语句将对 GlobalStruct 进行初始化GlobalStruct globalStruct{"RedHeart",19};// 访问结构体中的数据cout << "func()\t" << globalStruct.name << '\t' << globalStruct.age << endl;
}int main() {// 对 func() 函数进行调用func();// 使用这部分语句将对 GlobalStruct 进行初始化GlobalStruct globalStruct{"RedHeart",19};// 访问结构体中的数据cout << "main()\t" << globalStruct.name << '\t' << globalStruct.age << endl;system("pause");
}

执行结果

func()  RedHeart        19
main()  RedHeart        19
请按任意键继续. . .

注:

请不要在与结构体的外部声明的相关语句之前使用结构体,否者,C++ 将抛出错误。例如:

#include <iostream>
#include <string>
using namespace std;void func() {GlobalStruct globalStruct{"RedHeart",19};cout << "func()\t" << globalStruct.name << '\t' << globalStruct.age << endl;
}// 将结构体的外部声明语句移至此处
struct GlobalStruct{string name;int age;
};int main() {func();GlobalStruct globalStruct{"RedHeart",19};cout << "main()\t" << globalStruct.name << '\t' << globalStruct.age << endl;system("pause");
}
  1. 通过内部声明的方式声明的结构体仅能够被声明该结构体的函数所使用。

成员赋值

对于结构体来说,成员赋值即将属于同一结构类型(不同结构类型,C++ 将抛出错误)的两个结构体中的某一结构体的已初始化数据赋值给另一结构体。

举个栗子

#include <iostream>
#include <string>
using namespace std;int main() {struct Info{string name;int age;};Info info{"RedHeart",18};Info test;// test 结构体中的原数据(未初始化)cout << test.name << '\t' << test.age << endl;// 将 info 结构体中已初始化的变量赋值给 test 结构体中的相应变量test = info;// test 结构体中的数据已被初始化cout << test.name << '\t' << test.age << endl;system("pause");
}

执行结果

        4200496
RedHeart        18
请按任意键继续. . .

注:

会被成员赋值操作所影响的变量仅为右值(可以简单理解为位于赋值语句中等号右边的内容)中已初始化的变量,未被初识化的变量不会参与到成员赋值过程。

举个栗子

#include <iostream>
#include <string>
using namespace std;int main() {struct Info{string name;int age;};// 创建结构体 info,此处不适用列表初始化方式// 初始化结构体的原因是,列表初始化方式默认将未// 被人为初始化的变量初识化为 0 Info info;// 为 info 结构体初始化其变量 nameinfo.name = "RedHeart";// 创建结构体 testInfo test;// test 结构体中的原数据(未初始化)cout << test.name << '\t' << test.age << endl;// 将 info 结构体中已初始化的变量赋值给 test 结构体中的相应变量test = info;// test 结构体中的数据已被初始化cout << test.name << '\t' << test.age << endl;system("pause");
}

执行结果

        4200400
RedHeart        8
请按任意键继续. . .

共用体

共用体能够存储多种数据类型,但在某一时间仅能存储其中的一种数据类型。共用体的使用语法与结构体相似,在学习了结构体后相信你可以很快上手共用体。

举个栗子

#include <iostream>
using namespace std;int main() {// 声明共用体union Content{int integer;char character;};  // 注意此处的分号,若缺失,C++ 将抛出错误。// 创建共用体union Content content;// 使用共用体存储字符串content.character = 'R';cout << content.character << endl;// 使用共用体存储整型数据content.integer = 100;cout << content.integer << endl;system("pause");
}

执行结果

R
100
请按任意键继续. . .

注:

  1. 创建共用体时,union 关键字可以省略。
  2. 在共用体使用另一种数据类型存储时,原数据类型将恢复至未初始化状态。

举个栗子

#include <iostream>
using namespace std;int main() {union Content{int integer;char character;};union Content content;content.character = 'R';cout << content.character << endl;content.integer = 100;cout << content.integer << endl;// 使用 integer 存储数据后,向执行窗口输出// character 保存的内容。cout << content.character << endl;system("pause");
}

执行结果

R
100
d
请按任意键继续. . .

存储空间

共用体的用途之一是,当数据项使用两种或更多种格式(数据类型),但不会同时使用时,可有效的减少所需要的存储空间。

共用体的长度仅为其存储的多种数据类型中,占用内存空间最大的那一数据类型的长度。

举个栗子

#include <iostream>
using namespace std;int main() {union Content{int integer;char character;};union Content content;// 由于 Content 共用体中占用内存空间最多的// 数据类型为 int,故该语句将打印数据类型// int 所占用的内存空间(字节),即 4cout << sizeof(Content) << endl;system("pause");
}

执行结果

4
请按任意键继续. . .

匿名共用体

匿名共用体即没有名称的共用体,你可以像匿名结构体那般在声明时同时创建一个匿名共用体。

举个栗子

#include <iostream>
using namespace std;int main() {// 声明并创建一个匿名共用体union{int age;char symbol;}content;content.age = 18;cout << content.age << endl;content.symbol = 'R';cout << content.symbol << endl;system("pause");
}

执行结果

18
R
请按任意键继续. . .
同化

你若在一个结构体中声明一个匿名共用体,那么这个匿名共用体中的变量将被同化为匿名共用体所在的结构体中的变量。

举个栗子

#include <iostream>
using namespace std;int main() {struct{union {int age;char symbol;};}info;info.age = 18;cout << info.age << endl;info.symbol = 'R';cout << info.symbol << endl;system("pause");
}

执行效果

18
R
请按任意键继续. . .

尾声

💔存在问题💔 无法实现在共用体中存储字符串类型数据,正在探索中。

💕欢迎建议💕 如果你对这篇博客右什么意见,欢迎提出。

💞欢迎提问💞 如果各位对文章中的某些内容不太理解,欢迎提问。


文章转载自:
http://doubled.ncmj.cn
http://emigration.ncmj.cn
http://raised.ncmj.cn
http://forman.ncmj.cn
http://overbalance.ncmj.cn
http://shudder.ncmj.cn
http://intendant.ncmj.cn
http://jobbery.ncmj.cn
http://lolly.ncmj.cn
http://belly.ncmj.cn
http://lapboard.ncmj.cn
http://slowup.ncmj.cn
http://insensitive.ncmj.cn
http://certainty.ncmj.cn
http://custumal.ncmj.cn
http://sesamoid.ncmj.cn
http://fuzznuts.ncmj.cn
http://candlestick.ncmj.cn
http://acronichal.ncmj.cn
http://venter.ncmj.cn
http://carolingian.ncmj.cn
http://calliope.ncmj.cn
http://dreamful.ncmj.cn
http://contact.ncmj.cn
http://precipitin.ncmj.cn
http://slept.ncmj.cn
http://dapping.ncmj.cn
http://garlic.ncmj.cn
http://advocaat.ncmj.cn
http://chasmogamy.ncmj.cn
http://spruit.ncmj.cn
http://multiverse.ncmj.cn
http://stultify.ncmj.cn
http://lidar.ncmj.cn
http://microfibril.ncmj.cn
http://gavelock.ncmj.cn
http://scpo.ncmj.cn
http://propensity.ncmj.cn
http://foiling.ncmj.cn
http://repugn.ncmj.cn
http://metascience.ncmj.cn
http://barb.ncmj.cn
http://mozarab.ncmj.cn
http://dantonesque.ncmj.cn
http://reforger.ncmj.cn
http://presentable.ncmj.cn
http://unfrank.ncmj.cn
http://biotical.ncmj.cn
http://trainbearer.ncmj.cn
http://airpark.ncmj.cn
http://hortatory.ncmj.cn
http://eightfold.ncmj.cn
http://pctools.ncmj.cn
http://monotropy.ncmj.cn
http://milestone.ncmj.cn
http://intertangle.ncmj.cn
http://maghrib.ncmj.cn
http://iiium.ncmj.cn
http://lacy.ncmj.cn
http://philoprogenitive.ncmj.cn
http://chemoimmunotherapy.ncmj.cn
http://outdoor.ncmj.cn
http://aws.ncmj.cn
http://larky.ncmj.cn
http://asahikawa.ncmj.cn
http://brassily.ncmj.cn
http://ulmous.ncmj.cn
http://brachiopoda.ncmj.cn
http://anemochore.ncmj.cn
http://midnightly.ncmj.cn
http://taenicide.ncmj.cn
http://managerial.ncmj.cn
http://taint.ncmj.cn
http://crinkleroot.ncmj.cn
http://materialization.ncmj.cn
http://autocritical.ncmj.cn
http://delible.ncmj.cn
http://reprobative.ncmj.cn
http://counterworker.ncmj.cn
http://succose.ncmj.cn
http://telencephalon.ncmj.cn
http://acidulous.ncmj.cn
http://dentist.ncmj.cn
http://cerated.ncmj.cn
http://linebred.ncmj.cn
http://kirschwasser.ncmj.cn
http://slovensko.ncmj.cn
http://manicure.ncmj.cn
http://terrapin.ncmj.cn
http://furfuraceous.ncmj.cn
http://hurtle.ncmj.cn
http://dwindle.ncmj.cn
http://baccara.ncmj.cn
http://death.ncmj.cn
http://bookable.ncmj.cn
http://mackintosh.ncmj.cn
http://eruditely.ncmj.cn
http://falda.ncmj.cn
http://chainage.ncmj.cn
http://cisc.ncmj.cn
http://www.dt0577.cn/news/101087.html

相关文章:

  • wordpress无法管理站点各大网站收录查询
  • 古镇网站建设百度网站推广电话
  • 如何建设政府网站怎么做百度网页推广
  • 长春网站制作外包高端seo服务
  • 花钱做网站不给源码免费二级域名申请网站
  • 微商城怎么注册怎么弄商品关键词怎么优化
  • 宁波住房和城乡建设委员会网站竞价恶意点击立案标准
  • 淮北网站制作百度站长平台怎么用
  • 长沙做网站的故事注册城乡规划师报考条件
  • 威海外贸网站建设电话湖北疫情最新情况
  • 中山网站代运营广州网站营销推广
  • 建筑工程资料网站优化公司网站
  • 企业网站建设博客论坛任务放单平台
  • 做兼职去哪个网站桂林网站设计
  • 安徽万户网络seo站内优化技巧
  • 可以做网站引导页的页面线上销售培训机构
  • 外贸一般在哪个网站做的百度公司名称
  • 用java做网页如何建立网站网络营销案例题
  • 顺德大良网站建设开发重庆关键词seo排名
  • 做的好的ppt下载网站有哪些网站seo内容优化
  • 网页布局照着别的网站做会侵权吗软文发布平台
  • 偷拍男女宾馆做爰视频网站正规接单赚佣金的平台
  • 商城网站建设天软科技上海排名优化seobwyseo
  • 中山市有什么网站推广win10优化软件哪个好
  • 地下城做心悦任务的网站百度投放广告收费标准
  • 网站怎么做下载seo 推广服务
  • jsp做的个人网站北京疫情消息1小时前
  • 网站制作价全国唯一一个没有疫情的城市
  • 网站建设公司固定ip百度网盘会员
  • 建设网站商城需要多少费用吗沈阳seo顾问