int cards[4] = {3, 6, 8, 10}; // okay int hand[4]; // okay hand[4] = {5, 6, 7, 9}; // not allowed hand = cards; // not allowed
数组名本质是地址,那么将地址赋值给另一个地址这种操作本身就是错误的
数组名是一个指针,代表第一个元素的地址
编译器不会检查数组越界,数组名是数组的地址
二维数组
1 2 3 4 5 6 7 8 9 10 11 12 13
int a1[3][4] = { {0, 1, 2, 3}, // initializers for the row indexed by 0 {4, 5, 6, 7}, // initializers for the row indexed by 1 {8, 9, 10, 11} // initializers for the row indexed by 2 };
int a2[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11}; int a3[3][4] = {{ 0 }, { 4 }, { 8 }}; // 只初始化第一列,其余为 0 int a4[3][4] = {0, 3, 6, 9}; // 只初始化第一行,其余为0
// loop for (auto row : a1) for (auto col : row)
复杂声明
1 2 3 4
int *ptrs[10]; // ptrs is an array of ten pointers to int int &refs[10] = /* ? */; // error: no arrays of references int (*Parray)[10] = &arr; // Parray points to an array of ten ints int (&arrRef)[10] = arr; // arrRef refers to an array of ten ints
// char 字符串常用函数 strlen(p) // Returns the length of p, not counting the null. strcmp(p1, p2) // Compares p1 and p2 for equality. Returns 0 if p1 == p2,apositive value if p1 > p2, a negative value if p1 < p2. strcat(p1, p2) //Appends p2 to p1.Returns p1. strcpy(p1, p2) //Copies p2 into p1.Returns p1.
string s = "qwt"; char *str = s; // error: can’t initialize a char* from a string constchar *str = s.c_str(); // ok,c_str() 返回 C 风格字符串
扩展
1 2 3
int a1[3] = {0, 1, 2}; autoa2(a1); // a2 is an int* that points to the first element in a1 decltype(a1) a3 = {0, 1, 2};
C++11 新特性,新增了 begin() 和 end()
两个函数
1 2 3
int a[3] = {0, 1, 2}; int *beg = begin(a); // pointer to the first element in a int *last = end[a]; // pointer one past the last element in a
函数和数组
1
intsum_arr(int arr[], int n); // int arr[10],维度也可以写上
实际上 arr
传递的是数组指针,并且也是指向第一个元素的地址,所以也可以用下面的写法
1
intsum_arr(int *arr, int n);
在 C++ 中只有用于函数头或函数原型时,int *arr 和
int arr[] 的含义才是相同的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
intsum_arr(constint *begin, constint *end) { int total = 0; constint *pt; for (pt = begin; pt!=end; pt++) total += *pt; return total; }
intmain() { int cookies[7] = {0, 1, 2, 3, 4, 5, 6}; int sum = sum_arr(cookies, cookies + 7); return0; }
也能够引用传递
1 2 3 4 5
voidprint(int (&arr)[10]) { for (auto elem : arr) cout << elem << endl; // arr is a reference to an array of ten ints }
函数和二维数组
1 2
int data[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}}; int total = sum(data, 3);
sum() 的函数原型
1 2 3
intsum(int (*arr)[4], int size); // 或 intsum(int arr[][4], int size);
initializer_list
当不确定元素数量,且元素类型一致时,可以使用
initializer_list 进行初始化序列
1 2 3 4 5 6 7 8 9 10 11 12 13
initializer_list<T> lst; // Default initialization; an empty list of elements of type T.
initializer_list<T> lst{a,b,c...}; // lst has as many elements as there are initializers; elements are copies of the corresponding initializers. Elements in the list are const.
// Copying or assigning an initializer_list does not copy the elements in the list. After the copy, the original and the copy share the elements. lst2(lst); lst2 = lst;
lst.size(); //Number of elements in the list.
// Returns a pointer to the first and one past the last element in lst. lst.begin(); lst.end();