[英]Is it dumb to make a dynamic 2d array where the second dimension is a constant?
[英]Dynamic 2D array of strings where the second dimension can change
因此,我必须制作一个二维数组的字符串,称为history[][]
,其中history[i]
存储当前与实验i
有关的所有主题, history[i][j]
包含一个列出所有该特定主题已参与的实验。 问题是,我必须使用int* numsubjects
,这是一个整数数组,它告诉我实验i
有多少个主题。 但是,随着对象可以移至其他实验中, numsubjects
的内容应该能够移动。 我不知道该怎么做。 我不能使用vector
, dequeue
或list
。
experiments = 0;
numsubjects = new int[experiments+1];
numsubjects[experiments] = n;
history = new string*[0];
for(int j = 0; j < n; j++) {
history[0] = new string[j];
history[0][j] = "0";
}
上面的代码在只有一个实验(实验0)时会初始化所有内容。我需要一种以某种方式使用numsubjects创建历史记录的方法。
如果必须使用C样式的数组,则realloc()
将允许您调整数组的大小。
但是,由于这是C ++,因此我强烈建议您使用std::vector
或std::map
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.