简体   繁体   English

动态二维字符串数组,其中第二维可以更改

[英]Dynamic 2D array of strings where the second dimension can change

So I have to make a 2-d array of strings named history[][] , where history[i] stores all of the subjects currently involved in experiment i , and history[i][j] contains a string listing all of the experiments that this particular subject has been a part of. 因此,我必须制作一个二维数组的字符串,称为history[][] ,其中history[i]存储当前与实验i有关的所有主题, history[i][j]包含一个列出所有该特定主题已参与的实验。 The thing is, I have to use int* numsubjects , an array of integers that tells me how many subjects are in experiment i . 问题是,我必须使用int* numsubjects ,这是一个整数数组,它告诉我实验i有多少个主题。 However, the contents of numsubjects should be able to move around as subjects can be moved into other experiments. 但是,随着对象可以移至其他实验中, numsubjects的内容应该能够移动。 I have no idea how to go about doing this. 我不知道该怎么做。 I cannot use vector , dequeue , or list . 我不能使用vectordequeuelist

        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";
        }

The above code initializes everything when there is only one experiment, experiment 0. I need a way to make history somehow with numsubjects in it. 上面的代码在只有一个实验(实验0)时会初始化所有内容。我需要一种以某种方式使用numsubjects创建历史记录的方法。

If you must use C-style arrays, then realloc() will allow you to resize your array. 如果必须使用C样式的数组,则realloc()将允许您调整数组的大小。

However, since this is C++, I strongly encourage you to use std::vector or std::map . 但是,由于这是C ++,因此我强烈建议您使用std::vectorstd::map

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM