How does C ++ define an array with the number of unknown elements ?, Number Array
1. Dynamic Memory Allocation
C ++ dynamic memory application:
Int arraySize; // defines the number of elements.
Cin> arraySize; // number of input Elements
Int * p; // pointer to an array
P = new int [arraySize]; // dynamically allocates memory.
...
Delete [] p; // finally releases the memory space
C language dynamic memory application:
Int arraySize; // defines the number of elements.
Scanf ("% d", & arraySize); // number of input Elements
Int * p; // pointer to an array
P = (int *) malloc (arraySize * sizeof (int) // dynamically allocate memory
...
Free (p); // finally releases the memory space
2. vector
Vector is the Vector provided in C ++ STL.
The vector size can be dynamically changed.
The vector can access elements through subscript
You can use push_back to add elements to the vector and change the size.
# Include
Vector MyArray;
// You can use myArray. push_back (integer); to add elements based on data requirements. // you can use subscript to access Vector elements.