Compared with the vector type, an array has a significant defect in that the length of the array is fixed andProgramThe member cannot know the length of a given array. The array does not have the size operation to get its capacity, nor does it provide the push_back operation to automatically add elements. If you need to change the length of the array, the programmer can only create a larger new array, and then copy all elements of the original array to the new array space.
Currently, the C ++ program uses vector to replace arrays. arrays are used only when the performance test shows that vector cannot meet the necessary speed requirements.
An array is a composite data type consisting of the type name, identifier, and dimension. The type name specifies the type of elements stored in the array, while the dimension specifies the number of elements contained in the array.
Pointer ---- used to point to an object.
Suggestion: Avoid using pointers and arrays whenever possible.
Avoid using Uninitialized pointers.
If possible, do not define a pointer unless the object to which it is directed already exists. This avoids defining an uninitialized pointer.
If the pointer and the object to which it points must be defined separately, the pointer is initialized to 0 ,. Because the compiler can detect a pointer of 0, the program can determine that the pointer does not point to an object.
Void * pointer:
It can save the addresses of any type of objects. Only several limited operations are supported: Compare with another pointer; pass the void * pointer to the function or return the void * pointer from the function; assign a value to the other void * pointer.
Arithmetic Operation of pointer: The addition (or subtraction) of an integer N is equivalent to obtaining a new pointer. The new Pointer Points to (or before) an element pointed to by the pointer) the Nth element.
There are three important restrictions on array-type variables: the length of the array is fixed, and the length must be known during compilation. The array only exists in the block statement that defines it.
The dynamically allocated array will exist until the program releases it dynamically.
Each program occupies an available memory space during execution and is used to store dynamically allocated objects. This memory space is called the free storage zone or heap of the program. The C language uses malloc and free, and C ++ uses new and delete.
Int * P = new int [10] (); // The array is initialized to 0.
Int * P = new int [10]; // The array is not initialized.
Delete [] P; // release
String st3 ("Hello World ");
Char * STR = st3.c _ STR (); // return the expression of the C-style string.
Use arrays to initialize a vector
Const size_t arr_size = 6;
Int int_arr [arr_size] = {0, 1, 2, 3, 4, 5 };
Vector <int> ivec (int_arr, int_arr + arr_size );
Vector <int> ivec (int_arr + 1, int_arr + 4); // int_arr [1], int_arr [2], int_arr [3]
C ++ does not have multi-dimensional arrays. Generally, multi-dimensional arrays are actually arrays of arrays.