0%

C++PrimerPlus_4.4结构简介

1. 4.4 结构简介

数组只能存储同一个类型的数据(如10个int,10个float)

结构是一个更灵活的数据格式,结构是用户定义的类型,结构声明定义了这种数据的数据属性。

  1. 定义结构描述:描述并标记了能够存储在结构中的各类数据类型

  2. 按描述创建结构变量(结构数据对象)

🌰 例子:

现在要创造一个类型struct来描述其产品,这个产品必须包含名称容量售价,下面的结构可以满足:

1
2
3
4
5
6
7
8
struct inflatable
{
char name[20];
float volume;
double price;

}

这个新的数据格式名为inflatable,在创建之后,就可以像创建char或者int类型的变量那样去创建inflatable类型的数据了。

由于hat的类型是inflatable,可以使用运算符(.)来访问各个成员,如hat.volume,hat.price等等。总之,hat是一个结构,而hat.double是一个double变量。

例如:

1
2
3
ìnflatable hat; // hat is a structure variable of tyle inflatable
inflatable woopie_cushion;

1.1. 在程序中使用结构

🌰 一个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// main.cpp
// structur
//
// Created by YujiaYANG on 2021/8/15.
//

#include <iostream>

struct inflatable
{
char name[20];
float volume;
double price;

};


int main() {
using namespace std;
inflatable guest =
{
"Glorious Gloria", // name
1.88, // volume
29.99, // price
};

inflatable pal =
{
"Audacious Arthur",
3.12,
32.99
};

cout << "Expand your guest list with " << guest.name;
cout << " and " << pal.name << "! \n";

cout << "You can have both for $";
cout << guest.price + pal.price << "!\n";

return 0;

}

一些注意的点:

  • C++不提倡使用外部变量,但提倡使用外部结构声明
  • 使用一个结构也可以这样写在一行inflatable hei = {"Dalphine",2.13,2.15};
  • pal.name[0] 是一个字符A,pal[0]没有意义(因为pal是一个结构,而不是数组)

1.2. C++结构初始化

inflatable hei = {"Dalphine",2.13,2.15}; 里的 等号(=)也是可省的
☑️ inflatable hei {"Dalphine",2.13,2.15};

其次,如果大括号内未包含任何东西,各个成员将被设置成零。

1
2
3
4
5
6
inflatable hei {};

// 输出
// hei.name =
// hei.price = 0
// hei.volume = 0

1.3. 结构可以将string类作为成员吗

用string代替char name[20] 可以吗?

🌰例如:

1
2
3
4
5
6
7
8
`struct inflatable // struct definition
{
std::string name; // 👈 重点是这个替换
float volume;
double price;

}

答案是OK哒,但要注意namespace的定义,那么将std申明放在struct定义之前,要么就在定义中写std::string嘿嘿

1.4. 其他结构属性

1.5. 结构可以传递(成员赋值)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

struct inflatable
{
char name[20];
float volume;
double price;

};

int main(){
inflatable hat {"heihei",1.14,2.15};
inflatable big;
big = hat;


cout << "big.name = " << big.name << endl;
cout << "big.price = " << big.price << endl;



return 0;
}


1.5.1. 初始化方法1 (不那么方便阅读)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

struct inflatable
{
string name;
float volume;
double price;

} mr_smith, ms_jone; // 同时完成定义结构和创建结构变量的工作,初始化为0

int main(){
mr_smith.name = "hei_smith";

cout << "mr_smith.name = " << mr_smith.name << endl;
cout << "mr_smith.price = " << mr_smith.price << endl;
return 0;
}

1.5.2. 初始化方法2 (不方便阅读)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

struct inflatable
{
string name;
float volume;
double price;

} mr_smith =
{
"mr_smith",
1.14,
2.53
};

int main(){
// mr_smith.name = "hei_smith";

cout << "mr_smith.name = " << mr_smith.name << endl;
cout << "mr_smith.price = " << mr_smith.price << endl;
return 0;

1.5.3. 也可以声明没有名称的结构类型(有点类似于用一次就行)

这样将单纯创建一个名为position的结构变量,可以使用成员运算符(.)来访问它的成员position.x等等,但这种变量无名称,以后无法创建其副本,本书不使用此结构。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

struct
{
int x;
int y;
} position;

int main()
{
position.x = 2;
position.y = 4;

cout << "Position.x = " << position.x << endl;
cout << "Position.y = " << position.y << endl;

return 0;

}

1.6. 结构数组

可以创造一个元素为inflatable的数组(array):

例如要创建一个包含100个inflatable结构的数组gifts,可以这样做

1
2
3
inflatable gifts[100];
cin >> gifts[0].volume;
cout << gifts[99].price << endl;

也可以创建一个数组guests,同时初始化它的两个inflatable元素:

1
2
3
4
5
6
inflatable guests[2] =
{
{"bambi",0.5,21.99},
{"Godzilla",2000, 565.99}
};

1.7. 结构中的位字段

p78

-------------本文结束感谢您的阅读-------------