Without delving too much into it, there are two "places" where you can allocate memory: The stack and the heap. Stack memory is allocated by the variables created by the programmer. If a varied number of elements is later needed, the place to get more memory is the heap. In C++, a dynamic array looks like:
1 2 3 4 5 6 7 8
|
int totalItems = 0;
cout << "Enter the total number of iterms: ";
cin >> totalItems;
int *myArray = new int[totalItems];
for (int i = 0; i < totalItems; i++)
{
myArray[i] = i;
}
|
That is the basics. You need a pointer that points to the first element of the array, and you need a variable to hold the total number of elements.
The above requires that you free the allocated memory (by means of the new operator) using the delete[] operator. If you pointer variable goes out of scope, you must ensure you freed the memory used by that pointer before that happened.
Stuff like memory leaks have pushed the STL to create what is usually called collections. The vector class is a basic form of collections. There are other, more ellaborated classes that serve higher purposes. For example there is map, which is a dictionary collection. There are others as well. Don't ask me about STL. I am no good at it.
If the above is just over your head, consider stepping back and start with the basics of programming and C++.