Array is a composite type. It consists of the type, name and dimension of the array. The dimension indicates the number of elements in the array, which must be greater than 0. The dimension must be known when compiling, so the dimension must be a constant expression.
unsigned cnt = 42; //Is not a constant expression constexpr unsigned cnt1 = 42; /*Is a constant expression (a variable declared as constexpr must be a constant and must be initialized with a constant expression)*/ int arr[10]; //An array of 10 integers int* parr[cnt1]; // An array of 42 integer pointers string bad[cnt]; //Wrong, because cnt is not a constant expression string strs[get_size()]; // When get_size() is correct only when it is constexpr, otherwise it is wrong
When defining an array, you must specify the type of the array. You can't deduce it with the auto keyword. No referenced array exists.
By default, the elements of the array are initialized by default.
Explicitly initialize array:
const unsigned sy = 3; int a1[sy] = { 0,1,2 }; //The array with dimension 3 has three values: 0, 1 and 2 int a2[] = { 0,1,2 }; //The dimension is 3, which can be written like this. Its space size is determined by how much you assign int a3[4] = { 0,1,2 }; //Equivalent to A3 [] = {0,1,2,0}; Subsequent uninitialized values are initialized to 0 by default string a4[3] = { "hi","sy" }; //Equivalent to A4 [] = {"Hi", "sy", ""} int a5[2] = { 0,1,2 }; //Error, too many initial values, exceeding the size of array space
When we are actually writing programs, we must pay attention to the problem of subscript cross-border. This problem is often easy to be ignored and not easy to be found.
In the standard library, the array cannot be copied and assigned:
int a[] = {0,1,2}; int a1[] = a ;//error a2 = a ;//error
Complex array declaration:
int arr[10] = { 0,1,2,3,4,5 }; int* ptrs[10]; //ptrs is an array of 10 integer pointers int& sy[10]; //Error: no referenced array exists int(*parr)[10] = &arr; //parr points to an array of 10 integers int(&parr1)[10] = arr; //parr1 refers to an array of 10 integers
There is no special limit on the number of modifiers:
int *( &parr ) [10] = arr; //parr is a reference to an array that contains 10 pointers
The best way to understand the declaration of a complex array is to read it from the name of the array from the inside out.
Access array elements: the array can be accessed with the range for or subscript operator. Remember that the subscript of the array starts from 0 and does not cross the boundary.
When using array subscripts, they are usually defined as size_t type. It is a machine related unsigned type. It is designed large enough to represent the size of any object in memory. It is defined in the header file cstddef.
int arr[ ] = { 0,1,2,3,4,5,6,7,8 }; for (size_t i : arr) //size_t it's best to use auto auto derivation there { cout << i << endl; }
Pointers and arrays
We can use the address fetching character to obtain the pointer of an object, and the address fetching character can act on any object. Using the subscript operator on the array obtains the object of the array, and using the address character is to obtain the pointer of this element:
string nums[] = { "one ", "two " , "three" }; string* p = &nums[0]; string* p1 = nums; //This is equivalent to string* p1 = &nums[0]; Directly use the name of the array. The name of the array is the pointer to the first element of the array cout << *nums << endl;//Dereference the first pointer of the array to get the first element of the array cout << nums << endl; //Output the address of the first pointer of the array
When we use array as the initial value of auto variable, the derived type is pointer, not array:
int ia [] = {0,1,2,3}; auto ia1(ia); //ia1 is an integer pointer to the first element of ia ia1 = 41 ; //Error, the above derivation is a pointer type and cannot assign a value to a pointer with an integer value //The derivation above is in the form of auto IA1 (& ia [0]);
When we use the decltype keyword (select and return the type of operand), the above conversion will not occur:
int ia[] = { 0,1,2,3 }; decltype(ia) ia1; //ia1 is defined by the type returned by decltype. All ia1 is an integer array with dimension 4 ia1[1] = 999; //Assign a value to the second element of array ia1, and the subscript starts from 0
Pointers are also iterators, and there are also iterator operations:
int arr[10] = { 1,2,3,4,5,6,7,8,9 }; int* p = arr; for (size_t i = 0; i < sizeof(arr)/4; i++) { cout << *p << " "; p++; }
Standard library functions begin and end:
C++11 introduces two new functions named begin and end, which take the array as their parameters. Begin is responsible for returning the pointer to the first element of the array, and end is responsible for returning the pointer to the next position of the last element of the array, which conforms to the principle of left closing and right opening. They are defined in the iterator header.
int arr[10] = { 1,2,3,4,5,6,7,8,9 }; int* Begin_ = begin(arr); int* End_ = end(arr); while (Begin_ != End_) { cout << *(Begin_) << " "; Begin_++; }
When using these two library functions, note that the tail pointer cannot be dereferenced and incremented.
The result of the subtraction of two pointers is the distance between them. The pointers participating in the operation must point to the elements in the same array. The type of the result they want to subtract is called ptrdiff_t's standard library type, which is also defined in the cstddef header file. It is a signed type, which can be positive or negative.
Two null pointers are allowed to be subtracted, but they are not allowed to be added:
int* p = nullptr; int* i = nullptr; cout << p -i << endl; //The result is 0
Subscript and pointer:
The standard library type defines that the subscript used must be an unsigned type, but the built-in subscript operator does not have this restriction.
For example:
int ia[] = { 0,1,2,3,4,5 }; int* p = &ia[2]; //p points to the element with index 2 int j = p[1]; //p[1] is equivalent to * (p+1), that is, the element represented by ia[3] int k = p[-2]; //p[-2] is the element represented by ia[0]
Arrays also have two-dimensional data and arrays above two-dimensional. However, it is troublesome to have too many dimensions. Generally speaking, two-dimensional is enough for us to use.