Theoretically, is it enough if I only delete arrays and results but don't delete sub_array inside of arrays in the following code snippet, before return 0; or do I have to first delete all the sub_array before I can safely delete arrays and results.
int main() {
int subarrays, queries;
cin >> subarrays >> queries;
int** arrays = new int* [subarrays]();
int* results = new int[queries];
for (int i = 0; i < subarrays; i++) {
int length;
cin >> length;
int* sub_array = new int[length];
for (int j = 0; j < length; j++) {
int element;
cin >> element;
sub_array[j] = element;
}
arrays[i] = sub_array;
}
for (int i = 0; i < queries; i++) {
int query_from, arr_index;
cin >> query_from >> arr_index;
results[i] = arrays[query_from][arr_index];
}
for (int i = 0; i < queries; i++) {
cout << results[i] << endl;
}
return 0;
}
for (int i = 0; i < queries; i++) { delete[] arrays[i]; } delete[] arrays; delete[] results;char** argv). If, for example, you need a data structure with variable-length elements that can be removed, you might want a linked list if they’ll be accessed in order, or a hash table if you need constant-time random access. (Although you could implement a kind of unordered map this way, if the map is very dense.) You might also be able to use a “rectangular” array of fixed dimension. If you need a sparse matrix, compressed sparse row is more efficient.std::vector<std::string>or something.