I started learning C++ a week ago, coming from C.
The input I have is of the following format:
7
13 -4 -10 4 9 7 -3
4
0 -2 -1 2
2
3 5
0
The first number gives the number of elements in the first array. We stop scanning arrays once this number is a zero.
I would like to scan these arrays into an array of arrays as follows:
[[13,-4,-10,4,9,7,-3] , [0,-2,-1,2] , [3,5]]
I know how to scan in the first array:
int n;
int array1[MAXLENGTH];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> array1[i];
// scanf("%d", &array1[i]);
}
I get stuck on 0 -2 -1 2 since it starts with zero.
How can I scan in these arrays and stop once I encounter the last zero?
0 -2 -1 2?std::vectorinstead of array. Thestd::vectorcan grow dynamically, usingpush_back().vectorofvector. The primary reason is the capacity of each array is not known at compile time. To reduce the defects injected by dynamically allocating memory for the array, usestd::vector.