初始化2D数组C ++

时间:2014-12-10 01:45:27

标签: c++ arrays matrix

我的目标是在命令行读取文件,该文件是有向图的邻接矩阵表示,并将值存储在2D数组中。我的计划是读取一行上的整数数,并将其用作我的数组的大小(即5行输入,每行5个整数意味着我将创建一个5 x 5数组)。但是,要初始化数组,需要一个常量值,并计算每行的整数并将其存储在一个变量中以用作我的大小参数,这不允许我创建数组。

示例输入:

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

代码:

#include <iostream> 
#include <sstream>
#include <fstream>      

using namespace std;

int main(int argc, char **argv) 
{
    string currentLine;
    int i, m = 0;
    int count;
    ifstream input(argv[1]);
    int storage[10000];

    printf("Original matrix: \n" );
    if(input.is_open())
    {
        while(getline(input, currentLine))
        {
            istringstream iss(currentLine);
            count = 0;
            while(iss >> i)
            {
                if(iss.eof())//at end of each line, ends loop
                {
                    count++;
                    storage[m] = i;
                    m++;
                    printf("%d \n", i);
                    break;
                }
                else
                {
                    count++;
                    storage[m] = i;
                    m++;
                    printf("%d  ", i);
                }
            }
        }
    }

int **matrix;
    matrix = new int*[count]; 

    for(int y = 0; y < count; y++)
        matrix[y] = new int[count];

    for(int r = 0; r < count; r++)
        for(int c = 0; c < count; r++)
            matrix[r][c] = storage[r+c];

    printf("first = %d ", matrix[0][0]);


    system("PAUSE");
    return 0;
}

根据我的输入,我应该创建一个5 x 5阵列。但行

int matrix[count][count];

给我一​​个错误,说&#34;计数&#34; size参数应该是常量。我计算输入大小的方式是使用无效的方法,还是有办法创建一个常量用作我的大小参数?

3 个答案:

答案 0 :(得分:1)

不要使用2D原生C ++数组,而应考虑使用向量向量。

#include <vector>
#include <iostream>

int main() {
    using namespace std;

    int count = 5;

    // create a matrix of 'count' rows by 0 columns
    vector<vector<int>> matrix(count);

    // resize the column count for each row
    for (auto& r : matrix)
        r.resize(count);

    // use it just like an array
    for (int i = 0; i < count; ++i)
        matrix[i][i] = i;

    for (int r = 0; r < count; ++r) {
        for (int c = 0; c < count; ++c)
            cout << matrix[r][c] << ' ';
        cout << endl;
    }
}

答案 1 :(得分:0)

您需要使用动态数组

int *myArray;                //Declare pointer to type of array
myArray = new int[x];   //use 'new' to create array of size x
myArray[3] = 10;          //Use as normal (static) array
...
delete [] myArrray;       //remeber to free memeory when finished.

http://www.cplusplus.com/forum/beginner/1601/

http://www.cplusplus.com/doc/tutorial/dynamic/

答案 2 :(得分:0)

你不会得到二维阵列。您将不得不使用Array of Array。

如果您愿意使用动态分配,请执行以下操作:

int *myArray = new int [count];  // variable length array can get you into trouble here,
                                 // if you are not careful as the inner dimension needs
                                 // to be freed before the array goes out of scope.
for (/*iterate from 0 count*/) {
   myArray[i] = new int [count];
}


myArray[m][n] to give you a semblance of what you wanted.
相关问题