数组
1 //arrayone.cpp -- small arrays of integers
2 #include <iostream>
3
4 using namespace std;
5
6 int main() {
7 int yams[3];
8 yams[0] = 7;
9 yams[1] = 8;
10 yams[2] = 6;
11
12 int yamcosts[3] = {20, 30, 5}; //create and initialize
13 //int yamcosts[] = {20, 30, 5};
14 //int yamcosts[3] {20, 30, 5};
15
16 cout << "Total yams = " << cout << yams[0] << " + " << yams[1] << " + " << yams[2] << " = " <<
17 yams[0] + yams[1] + yams[2] << endl;
18
19 cout << "The package with " << yams[1] << " yams costs ";
20 cout << yamcosts[1] << " cents per yam.\n";
21
22 int total = yams[0] * yamcosts[0] + yams[1] * yamcosts[1];
23 total += yams[2] * yamcosts[2];
24
25 cout << "The total yam expense is " << total << " cents.\n";
26 cout << "\nSize of yams array = " << sizeof(yams) << " bytes.\n";
27 cout << "\nSize of yams array = " << sizeof yams << " bytes.\n";
28
29 cout << "Size of one element = " << sizeof(yams[0]) << " bytes.\n";
30 return 0;
31 }

字符串
char dog[8] = {'b', 'e', 'a', 'u', 'x', ' ', 'I', 'I'}; //not string,just a char array
char cat[8] = {'f', 'a', 't', 'e', 's', 's', 'a', '\0'};//a string
char cat[] = "fatessa";
1 //strings.cpp -- storing strings in an array
2 #include <iostream>
3 #include <cstring>
4
5 using namespace std;
6 int main() {
7 const int Size = 15;
8 char name1[Size];
9 char name2[Size] = "C+owboy";
10
11 cout << "Howdy! I'm " << name2 << "! What's your name?\n";
12 cin >> name1;
13 cout << "Well, " << name1 << ", your name has " << strlen(name1) << " letters and
14 cout << "in am array of " << sizeof(name1) << " bytes." << endl;
15 cout << "Your initial is " << name1[0] << ".\n";
16 name2[3] = '\0';
17 cout << "Here are the first 3 characters of my name: " << name2 << endl;
18 return 0;
19 }

cin输入字符串
cin使用空白(控制、制表符、换行符)确定字符串结束的位置。因此cin每次只输入一个单词。
getline()
cin.getline(name, 20);
getline通过换行符确定行尾。
get()
cin.get(name, 20).get()。
第一次调用get(name, 20)遇到换行符确定输入结束,但是换行符被保留着输入队列中,再次调用get()直接就遇到换行符,直接到结尾。所以在使用get(name, 20)之后紧接着调用一个get(),将输入队列里的换行符取出。
string类
需要引用string函数头,同样在std的名称空间里(std::string)。
“=” 赋值
"+" 拼接string
“+=” 附加
starchy(dest, src);
strcat(str1, str2);// str1 += str2
strlen(str)
其他
wchar_t
字符集无法用一个byte来表示,如日汉文系统。因为出现了wchar_t,是一个整型类型,可以表示系统使用的最大扩展字符集。通常加上前缀L来只是宽字符常量和宽字符串,iostream甚至提供了wcin和wcout。
wchar_t bob = L'P';
scout << L"tall" << endl;
在支持两字节wchar_t的系统中,每个字符存储在一个两字节的内存单元中。
通用字符名
类似于转义序列,为大量字符和符号提供标准数值编码。
C++11新增类型:char16_t、char32_t
这两种类型都是无符号的。
char16_t ch1 = u'q';
char32_t ch2 = U'\U0000222B';
bool
bool有true和false两种,可转换为int,true对应1,false对应0;
结构
结构体
1.声明结构体
struct inflatable {
char name[20];
float volume;
double price;
};
2.使用结构体定义变量
inflatable hat;
然后使用.来访问成员,例如hat.name.
3.初始化
inflatable hat = {
"Glorious Gloria",
1.88,
29.99
}; //和数组一样,使用逗号分隔值列表,并用大括号括起来。同样,在C++11中“=”可省略。
可以声明结构体数组,同样可以初始化。
共用体
关键字union。union是一种数据格式,能够存储不同的数据类型,但终能同时存储其中的一种类型。
1.声明
union one4all {
int int_val;
long long_val;
double double_val;
};
可以使用one4all变量存储int、long、double,但是是在不同的时间进行。
one4all pail;
pail.int_value = 115; //store an int
cout << pail.int_value;
pail.double_val = 1.38; //store a double, and int value is lost.
cout << pail.double_val;
union每次只能存储一个值,所以union的长度是最大成员的长度。使用union的可以节省空间,当数据项使用两种或者更多格式(不会同时使用)时。
struct widget {
char brand[20];
int type;
union id {
long id_num;
char id_char[20];
} id_val;
};
widget prize;
if(prize.type == 1) {
cin >> prize.id_val.id_num;
} else {
cin >> prize.id_val.id_char;
}
2.匿名共用体(anonymous union)没有名称,其成员将成为位于相同地址处的变量。每次只有一个成员是当前的成员。
struct widget {
char brand[20];
int type;
union {
long id_num;
char id_char[20];
};
};
if(prize.type == 1) {
cin >> prize.id_num;
} else {
cin >> prize.id_char;
}
由于union是匿名的,因此id_num和id_char被视为prize的两个成员,只是它们地址相同,因此不需要中间标志符id_val.
枚举enum
1.定义
相对const,enum提供了另一种创建符号常量的方式,允许定义新类型,但是有严格的限制。结构如下:
enum spectrum {red, orange, yellow, green, blue, violet, indigo, ultraviolet};
该语句完成两项工作:
- 让spectrum成为新类型的名称。
- 集那个red、orange、yellow等作为符号常量,默认从0开始赋值。这些叫做枚举常量。
2.声明
spectrum band;
3.赋值
band = blue;
band = 1; //1 - orange,but invalid,只能将同为spectrum类型的orange赋值给band,不能将int型的1赋值给band.
band = spectrum(1); //typecast 1 to type spectrum.
赋值只能选择定义的enum里面的值。
4.定义enum时可以显示设置枚举量的值:
enum bits {one = 1, two = 2, four = 4, eight = 8, nine};
此处的nine默认为eight的值+1.
早前,只能赋值int,现在可以使用long 和long long。
#include <iostream>
4 struct inflatable {
5 char name[20];
6 float volume;
7 double price;
8 };
9
10 union one4all {
11 int int_val;
12 long long_val;
13 double double_val;
14 };
15
16 using namespace std;
17 int main() {
18
19 inflatable bouquet = {
20 "sunflowers",
21 0.20,
22 12.49
23 };
24
25 inflatable choice;
26
27 cout << "bouguet: " << bouquet.name << " for $" << bouquet.price << endl;
28
29 choice = bouquet;
30 cout << "choice: " << choice.name << " for $" << choice.price << endl;
31
32 //struct array
33
34 inflatable guests[2] = {
35 {"Bambi", 0.5, 21.99},
36 {"Godzilla", 2000, 565.99}
37 };
38
39 cout << "The guests " << guests[0].name << " and " << guests[1].name << "\nhave a combined volume of "
40 << guests[0].volume + guests[1].volume << " cubic feet.\n";
41
42 //
43 struct torgle_register{
44 unsigned int SN : 4; //4 bits
45 unsigned int : 4;
46 bool goodIn : 1;
47 bool goodTorgle : 1;
48 };
49
50 //union
51 one4all pail;
52 pail.int_val = 15;
53 cout << pail.int_val << endl;
54 pail.double_val = 1.38;
55 cout << pail.double_val << endl;
指针和数组
指针
1.定义
指针是一个变量,存储的是值的地址。如何取到常规变量的地址呢,使用&取地址符可以获取位置。常规的内存地址表示是十六进制整数。
普通变量,值是指定量,地址是派生量。使用&来取地址。
指针变量,地址是指定量,值是派生量。使用*来取值。
对指针解除引用,意味着获取指针指向的值。
2.声明
int * p_updates;
p_updates指针指向int类型。p_updates是地址,p_updates是int。
int ptr; //ptr是int类型的值。C常规这样用。
int* ptr; //int* 是一种复合类型 —— 指向int的指针。C++常规这样用。
3.赋值
int higgens = 5;
int * pt = &higgens; //将pt的值设置为&higgens
4.危险
创建指针时,系统分配用来存储地址的内存,不会分配指针所指向的数据的内存,需要另外申请数据存储的空间。
数组
指针和数据基本等价。指针变量+1,地址后移一个。数组名也是地址(第一个元素的地址),下标+1,取下一个地址的值。
存储
自动存储
函数内部的常规变量
静态存储
static修饰的变量,和函数外部的常规变量。
动态存储
new、delete
vector和array
模版类vector
类似于string类,是一种动态数组,长度随着数据增减可变。
模版类array
固定长度的数组,相对普通数组,效率高,安全性强。
int (*p)(int a, int b); //p是一个指向函数的指针变量,所指函数的返回值类型为整型
int *p(int a, int b); //p是函数名,此函数的返回值类型为整型指针