c++ - 将文本文件中的整数矩阵存储到 C++ 中的数组中

标签 c++ arrays file loops matrix

我有一个名为 matrices.txt 的文件,其中包含两个 3 x 3 的矩阵。 它们是:

1 2 3
4 5 6
7 8 9
1 2 3 
4 5 6 
7 8 9

我正在尝试从该文件中读取并将其存储到数组 proto_matrix 中,以便稍后将其拆分为两个矩阵。

我遇到的问题是我无法将数字存储在数组中。

我的代码是

#include <iostream>
#include <fstream>

using namespace std;

void main()
{
    int i = 0, j, k = 0, n, dimension;
    cout << "Enter the dimension of square matrices (3 by 3 would be 3) \n";
    cin >> n;
    dimension = n * n;
    int proto_matrix[2 * dimension];

    // make array of two matrices combined, this will be split into two matrices
    ifstream matrix_file("matrices.txt");

    while(matrix_file)
    {
        matrix_file >> proto_matrix[i];
    }
    matrix_file.close();
}

我尝试调试代码,似乎数组中没有存储整数,只是随机数。

最佳答案

这个:

int proto_matrix[2 * dimension];

是可变长度数组 (VLA),它不是标准 C++,但受某些编译器扩展支持。如果用g++ -pedantic main.cpp编译,肯定会报错。

这意味着如果您不得不使用数组,则需要动态分配内存。

虽然你真的不知道,C++ 提供了 std::vector ,这可以相对轻松地实现您想要的。我说相对,因为一个文件中有两个矩阵,这使得解析文件有点棘手。通常我们将一个矩阵放在一个文件中,将另一个矩阵放在另一个文件中。

  • 二维数组可以表示为二维 vector 。
  • 您需要两个矩阵,因此需要两个二维 vector 。

因此,我将使用大小为 2(固定大小!)的数组,其中每个元素都将是一个二维 vector 。

此外,我将跟踪我现在正在从文件中读取哪个矩阵(第一个或第二个),以及我当前读取了多少行,以便填充矩阵的正确单元格。

index 将等于 0 或 1,以索引数组。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

int main()
{
    int i = 0, j, k = 0, n, dimension;
    cout << "Enter the dimension of square matrices (3 by 3 would be 3) \n";
    cin >> n;
    dimension = n * n;
    vector< vector<int> > v[2];
    v[0].resize(n);
    v[1].resize(n);
    // make array of two matrices combined, this will be split into two matrices
    ifstream matrix_file("matrices.txt");
    string line;
    int rows_read = 0, cols_read = 0, index = 0;
    while (std::getline(matrix_file, line))
    {
        std::istringstream iss(line);
        int a, b, c;
        if (!(iss >> a >> b >> c)) { break; } // error
        // process tuple (a, b, c)
        if(index == 0 && rows_read >= n)
        {
            rows_read = 0;
            index = 1;
        }
        //cout<<"rows = " << rows_read << " " << index<<endl;

        v[index][rows_read].push_back(a);
        v[index][rows_read].push_back(b);
        v[index][rows_read++].push_back(c);
    }
    matrix_file.close();

    for(int i = 0; i < 2; ++i)
    {
        cout << "Printing matrix " << i << endl;
        for(auto& matrix: v[i])
        {
            for(auto& number: matrix)
                cout << number << " ";
            cout << endl;
        }
    }
    return 0;
}

输出:

Printing matrix 0
1 2 3 
4 5 6 
7 8 9 
Printing matrix 1
1 2 3 
4 5 6 
7 8 9 

PS:与你的问题无关,但是What should main() return in C and C++?一个 int

关于c++ - 将文本文件中的整数矩阵存储到 C++ 中的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47560229/

相关文章:

c++ - 是否有任何中间件/库可以将您的二进制或文本数据从 64 位转换为 32 位?

c++ - 实现windows窗体c++选项卡选择事件

java - 为什么我收到 "bad operand types for binary operator"错误?

java - 在java中写入文件期间的IOException处理

python - 为什么我得到这个输出? (Python)

linux - 写入许多文件的替代方案。 MongoDB?

c++ - 防止多个 shared_ptr 指向同一个对象的最佳方法

c++ - 可中断排序函数

c - 在 C 中保存 16 位二进制数据类型

javascript - Angular 2 数组 : categorize into data sets based on user input