I have the following structure:
typedef struct _DynamicArray {
int *data = nullptr;
_DynamicArray *ptr_next = nullptr;
_DynamicArray *ptr_dim = nullptr;
} DynamicArray; // so that every matrix cell contains another matrix cell
And then the following recursive method:
void _BuildArray(std::string const& source, StringIterator& sit, DynamicArray *dArray, bool& error) {
if (!error) {
while (sit+1 < source.length()) {
++sit;
switch (source[sit]) {
case '[':
dArray->ptr_dim = new DynamicArray();
_BuildArray(source, sit, dArray->ptr_dim, error);
break;
case ']':
return;
case ',':
break;
case ' ':
break;
default:
std::string str;
while (std::isdigit(source[sit])) {
str.push_back(source[sit]);
++sit;
}
--sit;
if (str.empty()) {
error = true;
return;
}
else {
dArray->data = new int(stoi(str));
dArray->ptr_next = new DynamicArray();
dArray = dArray->ptr_next;
}
break;
}
}
}
}
And then if I pass "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]" as a parameter, it builds the following flatten: "[1,2,6,7,8]" (instead of "[1,2,3,4,5,6,7,8]"). Why?
The calling snippet is this:
StringIterator sit = 0;
bool error = false;
this->dynArray = new DynamicArray();
_BuildArray(this->listString, sit, this->dynArray, error);
std::stringand then you want to complicate life yourself with raw pointers while you have so many facilities in C++ likestd::list,std::vector..