You have two ways: first use a zero-size array, after each input you delete the array and select a new one that is greater than +1, and then save the input. It uses less memory but is inefficient. (In C, you can use realloc
to increase efficiency)
The second is to use a buffer, for example, you save data entry in an array of a fixed size, and when it is full, you add a buffer to the end of the main array (by deleting and redistributing).
By the way, you can use std::vector
, which automatically and effectively increases the size of itself.
source share