简体   繁体   中英

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. The thing is, I have to use int* numsubjects , an array of integers that tells me how many subjects are in experiment i . However, the contents of numsubjects should be able to move around as subjects can be moved into other experiments. I have no idea how to go about doing this. I cannot use vector , dequeue , or 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";
        }

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.

If you must use C-style arrays, then realloc() will allow you to resize your array.

However, since this is C++, I strongly encourage you to use std::vector or std::map .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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