C ++打印出int **数组?

时间:2016-04-02 07:02:55

标签: c++ arrays pointer-to-pointer

这是一个初学者的问题,但我只想尝试在这里指点指针。

如果我创建一个这样的数组数组:

    int n; // n lines
    cin>>n; // read in first line, number of lines in array of arrays
    int **arrays =new int *[n];  // create pointer of pointer of size n
    for (int i = 0; i < n; i++) {  // looping through n lines to fill each
        int sz;  // first int of each line is line size
        cin>>sz; // read in line size
        int *line = new int [sz];  // create line of size sz
        for (int j = 0; j < sz; j++) {  // loop through individual ints in line
            int x; // declare individual int
            cin>>x; // read in individual int
            line[j] = x; // fill line with each x
        }
        *(arrays+i)=line; // fill lines of 2D array
    }

/*
Sample input:

2       // n lines 
3 1 5 4  // first int of each line is line length
5 1 2 8 9 3 
*/

我创建int **arrays之后是否有任何方法可以在每个子阵列的长度上获取信息,例如strlen()int*&#39} s,以便我可以使用双for循环打印出来?或者,如果我想打印出来,我是否需要首先跟踪每一行的长度?

0 个答案:

没有答案