参考总结
C++ Primer Plus (第6版)
数组
数组是一种数据格式,能存储同一类型的值,每个值都存储在一个独立的数组元素中,计算机内存中依次存储数组各个元素
定义初始化
一维数组声明
typeName arrayName[arraySize]
short months[12];
一维数组初始化规则
- 只有定义数组的时候才能初始化
int cards[5] = { 1,2,3,4,5 }; // [正确] int hands[5]; hands[5] = {2,4,6,8,10}; // [不允许] hands = cards; // [不允许]
- 只对一部分的数组元素赋值,其它的默认为 0,char类型默认为空
float total[50] = {5.0,4.5}; // 第一个和第二个元素为5.0和4.5,其他默认为0 long all[500] = {0}; // 全部元素为0
- 初始化数组方括号为空,则c++编译器自己计算个数
自己计算数组大小short things[] = {1, 5, 3, 8};
short things[] = {1, 5, 3, 8}; int num_elements = sizeof (things) / sizeof (short); // 4
二维数组声明
typeName arrayName[arraySize][arraySize]
int maxtemps[4][5];
二维数组初始化
在一维数组的基础上
int maxtemps[4][5] =
{
{1,200},
{300,200,300,100,40},
{30,40,50,60},
{1000}
};
结构数组声明
也就是typeName不是基本类型, 是结构体或者是类
struct inflatable
{
char name[20];
float volume;
double price;
};
inflatable guests[3];
结构数组初始化
inflatable guests[2] =
{
{"zb", 0.5f, 21.99},
{"zzbbzb", 2000.0f, 454.23}
}
指针与数组
动态数组创建
type_name * pointer_name = new type_name [num_elements]
int* psome = new int [10];
delete [] psome;
需要手动销毁
动态数组使用
int* psome = new int[3]; // psiome指向第一个元素int[0]地址
psome[0] = 333;
psome[1] = 2;
psome[2] = 99;
cout << *psome << endl; // 333
cout << *(psome + 1) << endl; // 2
cout << psome[0] << endl; // 333
cout << psome[1] << endl; // 2
psome++; // 把psomez指向第二个元素int[1]的地址
cout << psome[0] << endl; // 2
cout << psome[1] << endl; // 9
psome--; // 指针指向原来的地址,delete才能正确的地址
delete [] psome;
在大多数情况下,c++ 将数组名视为数组的第一个元素的地址
一种例外情况是,sizeof 数组名,返回整个数组的长度
double wages[3] = { 1000.1,2000.2,3000.3 };
double * pw = wages; // 指向第一个元素的地址
double * pw2 = &wages[0]; // 获得第一个元素的地址赋值
这两个指针指的都是同一个位置, 只是赋值方式不同,也就是可以有等式
wages = &wages[0]
对数组名应用地址运算符,得到整个数组的地址
short tell[5] = {1,2,3,4,5};
// 从概念上来讲
// tell == &tell[0] 是指一个2字节内存的地址
// &tell 是指一个20字节内存块的地址
cout << tell << endl;
cout << &tell << endl;
short (*p)[5] = &tell; //指一个含有5个元素的数组指针 指向 一个数组地址(一个20字节内存块的地址)
short *pp = tell;
short *t[5]; // 指含有5个short指针的数组
for (int i = 0; i < 5; i++)
{
t[i] = &tell[i];
}
for (int i = 0; i < 5; i++)
{
cout << *(t[i]) << endl; // 1,2,3,4,5
}
for (int i = 0; i < 5; i++)
{
cout << (*p)[i] << endl; // 1,2,3,4,5
}
for(int i = 0; i < 5; i++)
{
cout << pp[i] << endl; // 1,2,3,4,5
cout << *(pp + i) << endl; // 1,2,3,4,5
}
指针变量加1时,其增加的值等于指向的类型占用的字节数
数组可以用指针表示(必须理解)
int arr[3] = {1,2,3};
// arr[i] == *(arr + i)
// &arr[i] == arr + i
指针和二维数组
理解了指针和一维数组,可以对二维数组和指针进行了解
int a[3][2] = { {1,2},{3,4},{5,6} };
cout << a << endl; // 000000DFA676F098
cout << &a << endl; // 000000DFA676F098
cout << a[0] << endl; // 000000DFA676F098
cout << &a[0] << endl; // 000000DFA676F098
cout << &a[0][0] << endl; // 000000DFA676F098
cout << a[0][0] << endl; // 1
int(*ap)[3][2] = &a;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
cout << (*ap)[i][j] << endl; // 1,2,3,4,5,6
}
}
int (*pp)[2] = &a[0]; // 相当于 int(*pp)[2] = a, pp + 1 也就是移动 sizeof(int) * 2 的距离
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
cout << (*(pp+i))[j] << endl; // 1,2,3,4,5,6
cout << *(*(pp+i) + j) << endl; // 1,2,3,4,5,6
cout << pp[i][j] <<endl; // 1,2,3,4,5,6
}
}
int *ppp = &a[0][0]; // 要了解数组在内存中是连续的
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
cout << (*(ppp + i * 2 + j)) << endl; // 1,2,3,4,5,6
}
}
下一篇:c++基础 数组(二)