C++ 矩阵到动态二维数组

标签 c++ arrays pointers matrix dynamic-arrays

假设我们在 matrix.txt 文件中有一个矩阵,存储方式如下:

Image1

我们想把它变成:

Image 2

数字 8(第一个数字)表示二维数组有多大。之后它的意思是: 1连接到2(connection的值为1,永远为1) 1 连接到 8 3 连接到 4

当转换为 2D 动态数组时,我们希望 ARRRAY 0,1...0,7...2,3 中的值 1 等等(我没有使用方括号,因为 stackoverflow 将它们读取为链接)。

    int number;
int **a = new int*[number];
for (int i = 0; i<number; i++) {
    a[i] = new int[number];
}
for (int i = 0; i<number; i++) {
    delete[]a[i];
}
    delete[]a;

string line;
ifstream myfile("matrix.txt");
if (myfile.is_open())
{
    getline(myfile, line);
     istringstream(line)>> number; 


    while (getline(myfile, line)){
        cout << line << '\n';
        //HERE I SHOULD TURN THOSE NUMBERS INTO VALUES IN 2D ARRAY
    }
    myfile.close();
}

所以我的问题是:如何将这些数字转换为二维数组中的矩阵?

谢谢

最佳答案

最简单但可能不是最快的方法是将此行写入 std::stringstream ,然后从字符串流读回行、列和值变量。如果您正在从文件中读取,首先读取文件的成本通常比以缓慢的方式解析文件的成本相形见绌。如果这对您的情况很重要(并首先分析代码以确保它确实如此),请考虑手动解析文件。也就是说,这个基本逻辑将成立。

while (getline(myfile, line)){
    cout << line << '\n';
    std::stringstream linestream(line);
    int row;
    int column;
    int value;
    if (linestream >> row >> column >> value)
    {
        a[row-1][column-1] = value;
        a[column-1][row-1] = value;// mirror
    }
    else
    {
        // handle file formatting error
    }
}

题外话,考虑使用矩阵类为您管理 a 而不是原始二维数组。 The matrix class here at isocppp.org is good and fast ,以及一些非常好的通用建议。

上面带有 isocpp 矩阵的代码如下所示:

while (getline(myfile, line)){
    cout << line << '\n';
    std::stringstream linestream(line);
    int row;
    int column;
    int value;
    if (linestream >> row >> column >> value)
    {
        a(row-1,column-1) = value;
        a(column-1,row-1) = value;// mirror
    }
    else
    {
        // handle file formatting error
    }
}

几乎相同且更易于使用,因为您不必担心自己管理内存、传递数组维度或一些错误代码(例如 a[4] = 0; ) 对数组的一行进行核对。

附录

这段代码

int number;
int **a = new int*[number];
for (int i = 0; i<number; i++) {
    a[i] = new int[number];
}
for (int i = 0; i<number; i++) {
    delete[]a[i];
}
delete[]a;

有两个严重的问题:

  1. a 的大小为 numbernumber 尚未分配。 number 可以是任何东西,从立即致命的负数(不能有负数大小的数组)到可能致命的巨大数字(您的计算机有 9,223,372,036,854,775,807 平方字节的 RAM?没想到.)
  2. 它在分配存储后立即删除它。释放内存是一个很好的养成习惯,但最好在使用内存后释放内存,而不是之前。

所以:

// define `a` here
string line;
ifstream myfile("matrix.txt");
if (myfile.is_open())
{
    getline(myfile, line);
    istringstream(line)>> number; 

    // allocate storage for `a` here

    while (getline(myfile, line)){
        cout << line << '\n';
        //line reading code goes here
    }
    myfile.close();
}
// delete `a` somewhere down here after it's been used.

关于C++ 矩阵到动态二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36750357/

相关文章:

c++ - 错误 : invalid in-class initialization of static data member of non-integral type 'const char[]'

c++ - 类链中的转换

c++ - 尝试反转 C++ 数组并同时将其存储到新数组中

c - 从单链表中删除条目

c++ - 函数c++中指针之间的相等性

c++ - Objective-C++ 无法使用 MyType *__strong 类型的左值初始化类型为 id<...> 的参数,即使它符合协议(protocol)

c++ - 如何制作xls图表?

ruby - 在 Ruby 中处理单个值或数组的最佳方法是什么

ios - Swiftt3:如果不重复,则追加到数组的扩展

从结构指针复制到C中数据库的结构指针