參考總結
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++基礎 數組(二)