C/C++ 运行时多维动态数组
C/C++ Runtime Multidimensional Dynamic Array
我有以下结构:
typedef struct _DynamicArray {
int *data = nullptr;
_DynamicArray *ptr_next = nullptr;
_DynamicArray *ptr_dim = nullptr;
} DynamicArray; // so that every matrix cell contains another matrix cell
然后是下面的递归方法:
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;
}
}
}
}
然后如果我通过“[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] ”作为参数,它构建以下扁平化:“[1,2,6,7,8]”(而不是“[1,2,3,4,5,6,7,8]”)。为什么?
调用代码段是这样的:
StringIterator sit = 0;
bool error = false;
this->dynArray = new DynamicArray();
_BuildArray(this->listString, sit, this->dynArray, error);
一旦递归 _BuildArray
调用 returns,您就不会像在 default:
情况下那样推进 dArray
。这意味着您遇到的下一个 [
将覆盖前一个 [
.
的结果
这个答案只是为了澄清我的评论,我说的是一种利用 C++ STL 结构来避免手动管理分配和动态内存的结构,例如:
class CellVisitor
{
public:
virtual accept(Cell* cell) = 0;
};
class Cell
{
public:
virtual void visit() = 0;
};
class ContainerCell : public Cell
{
private:
std::vector<std::unique_ptr<Cell>> cells;
public:
void addCell(...) { ... }
void visit(CellVisitor* visitor) override
{
visitor->accept(this);
for (auto& cell : cells)
cell->visit();
}
};
class IntegerCell : public Cell
{
private:
std::vector<int> data;
public:
void visit(CellVisitor* visitor) override
{
visitor->accept(this);
}
}
我有以下结构:
typedef struct _DynamicArray {
int *data = nullptr;
_DynamicArray *ptr_next = nullptr;
_DynamicArray *ptr_dim = nullptr;
} DynamicArray; // so that every matrix cell contains another matrix cell
然后是下面的递归方法:
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;
}
}
}
}
然后如果我通过“[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] ”作为参数,它构建以下扁平化:“[1,2,6,7,8]”(而不是“[1,2,3,4,5,6,7,8]”)。为什么?
调用代码段是这样的:
StringIterator sit = 0;
bool error = false;
this->dynArray = new DynamicArray();
_BuildArray(this->listString, sit, this->dynArray, error);
一旦递归 _BuildArray
调用 returns,您就不会像在 default:
情况下那样推进 dArray
。这意味着您遇到的下一个 [
将覆盖前一个 [
.
这个答案只是为了澄清我的评论,我说的是一种利用 C++ STL 结构来避免手动管理分配和动态内存的结构,例如:
class CellVisitor
{
public:
virtual accept(Cell* cell) = 0;
};
class Cell
{
public:
virtual void visit() = 0;
};
class ContainerCell : public Cell
{
private:
std::vector<std::unique_ptr<Cell>> cells;
public:
void addCell(...) { ... }
void visit(CellVisitor* visitor) override
{
visitor->accept(this);
for (auto& cell : cells)
cell->visit();
}
};
class IntegerCell : public Cell
{
private:
std::vector<int> data;
public:
void visit(CellVisitor* visitor) override
{
visitor->accept(this);
}
}