[英]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.